Skip to content

Commit 3c60492

Browse files
[py] Simplify print pdf tests
This introduces a new headless target and simplifies the tests into only one test file.
1 parent 6e545d8 commit 3c60492

File tree

4 files changed

+74
-79
lines changed

4 files changed

+74
-79
lines changed

py/BUILD.bazel

+43-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ py_test_suite(
215215
"test/selenium/webdriver/chrome/**/*.py",
216216
"test/selenium/webdriver/common/**/*.py",
217217
"test/selenium/webdriver/support/**/*.py",
218-
]),
218+
],
219+
exclude=["test/selenium/webdriver/common/print_pdf_tests.py"]
220+
),
219221
args = [
220222
"--instafail",
221223
"--driver=Chrome",
@@ -246,6 +248,46 @@ py_test_suite(
246248
],
247249
)
248250

251+
py_test_suite(
252+
name="test-chrome-headless",
253+
size="large",
254+
srcs=glob([
255+
"test/selenium/webdriver/chrome/**/*.py",
256+
"test/selenium/webdriver/common/**/*.py",
257+
"test/selenium/webdriver/support/**/*.py",
258+
],
259+
),
260+
args=[
261+
"--instafail",
262+
"--driver=Chrome",
263+
"--headless=true"
264+
],
265+
tags=[
266+
"no-sandbox",
267+
],
268+
deps=[
269+
":init-tree",
270+
":selenium",
271+
":webserver",
272+
requirement("attrs"),
273+
requirement("idna"),
274+
requirement("iniconfig"),
275+
requirement("importlib_metadata"),
276+
requirement("h11"),
277+
requirement("more_itertools"),
278+
requirement("multidict"),
279+
requirement("outcome"),
280+
requirement("pluggy"),
281+
requirement("py"),
282+
requirement("pytest"),
283+
requirement("pytest-instafail"),
284+
requirement("pytest-trio"),
285+
requirement("sortedcontainers"),
286+
requirement("sniffio"),
287+
requirement("zipp"),
288+
],
289+
)
290+
249291
py_test_suite(
250292
name = "test-edge",
251293
size = "large",

py/conftest.py

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def pytest_addoption(parser):
5353
help='location of the service executable binary')
5454
parser.addoption('--browser-args', action='store', dest='args',
5555
help='arguments to start the browser with')
56+
parser.addoption('--headless', action='store', dest='headless',
57+
help="Allow tests to run in headless")
5658

5759

5860
def pytest_ignore_collect(path, config):
@@ -112,6 +114,8 @@ def fin():
112114
if driver_instance is None:
113115
if driver_class == 'Firefox':
114116
options = get_options(driver_class, request.config)
117+
if driver_class == 'Chrome':
118+
options = get_options(driver_class, request.config)
115119
if driver_class == 'Remote':
116120
capabilities = DesiredCapabilities.FIREFOX.copy()
117121
kwargs.update({'desired_capabilities': capabilities})
@@ -136,6 +140,7 @@ def fin():
136140
def get_options(driver_class, config):
137141
browser_path = config.option.binary
138142
browser_args = config.option.args
143+
headless = bool(config.option.headless)
139144
options = None
140145

141146
if driver_class == 'ChromiumEdge':
@@ -152,6 +157,12 @@ def get_options(driver_class, config):
152157
if browser_args is not None:
153158
for arg in browser_args.split():
154159
options.add_argument(arg)
160+
161+
if headless:
162+
if not options:
163+
options = getattr(webdriver, f"{driver_class}Options")()
164+
165+
options.headless = headless
155166
return options
156167

157168

py/test/selenium/webdriver/chrome/print_pdf_tests.py

-59
This file was deleted.

py/test/selenium/webdriver/common/print_pdf_tests.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,33 @@
2121
END_INDEX = 5
2222
PDF_MAGIC_NUMBER = 'JVBER'
2323

24-
@pytest.mark.xfail_chrome(reason='chrome needs a headless driver instance.', run=False)
24+
2525
def test_pdf_with_2_pages(driver, pages):
26-
firefox_print_options = PrintOptions()
27-
firefox_print_options.page_ranges = ['1-2']
26+
print_options = PrintOptions()
27+
print_options.page_ranges = ['1-2']
28+
29+
pages.load("printPage.html")
30+
31+
base64code = driver.print_page(print_options)
2832

29-
driver.get(pages.url("printPage.html"))
30-
firefox_base64code = driver.print_page(firefox_print_options)
33+
assert base64code[START_INDEX: END_INDEX] == PDF_MAGIC_NUMBER
3134

32-
assert firefox_base64code[START_INDEX : END_INDEX] == PDF_MAGIC_NUMBER
3335

34-
@pytest.mark.xfail_chrome(reason='chrome needs a headless driver instance.', run=False)
3536
def test_pdf_with_all_pages(driver, pages):
36-
driver.get(pages.url("printPage.html"))
37-
firefox_base64code = driver.print_page()
37+
pages.load("printPage.html")
38+
base64code = driver.print_page()
39+
40+
assert base64code[START_INDEX: END_INDEX] == PDF_MAGIC_NUMBER
3841

39-
assert firefox_base64code[START_INDEX : END_INDEX] == PDF_MAGIC_NUMBER
4042

41-
@pytest.mark.xfail_chrome(reason='chrome needs a headless driver instance.', run=False)
4243
def test_valid_params(driver, pages):
43-
firefox_print_options = PrintOptions()
44+
print_options = PrintOptions()
45+
46+
print_options.page_ranges = ['1-2']
47+
print_options.orientation = 'landscape'
48+
print_options.width = 30
4449

45-
firefox_print_options.page_ranges = ['1-2']
46-
firefox_print_options.orientation = 'landscape'
47-
firefox_print_options.width = 30
48-
49-
driver.get(pages.url("printPage.html"))
50-
firefox_base64code = driver.print_page(firefox_print_options)
50+
pages.load("printPage.html")
51+
base64code = driver.print_page(print_options)
5152

52-
assert firefox_base64code[START_INDEX : END_INDEX] == PDF_MAGIC_NUMBER
53+
assert base64code[START_INDEX: END_INDEX] == PDF_MAGIC_NUMBER

0 commit comments

Comments
 (0)