Skip to content

Commit 53eb2ba

Browse files
authored
Merge pull request #1363 from seleniumbase/window-size-option-and-logging-updates
Window Size option and logging output updates
2 parents cf59d53 + 3c38af3 commit 53eb2ba

File tree

11 files changed

+147
-14
lines changed

11 files changed

+147
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ The code above will leave your browser window open in case there's a failure. (i
489489
--devtools # (Open Chrome's DevTools when the browser opens.)
490490
--reuse-session | --rs # (Reuse the browser session between tests.)
491491
--crumbs # (Delete all cookies between tests reusing a session.)
492-
--maximize # (Start tests with the web browser window maximized.)
492+
--window-size # (Set the browser window size: "Width,Height".)
493+
--maximize # (Start tests with the browser window maximized.)
493494
--screenshot # (Save a screenshot at the end of each test.)
494495
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
495496
--external-pdf # (Set Chrome "plugins.always_open_pdf_externally": True.)

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
sb._reuse_session = False
7070
sb._crumbs = False
7171
sb.visual_baseline = False
72+
sb.window_size = None
7273
sb.maximize_option = False
7374
sb.save_screenshot_after_test = False
7475
sb.timeout_multiplier = None

examples/test_apple_site.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_apple_developer_site_webdriver_instructions(self):
2222
self.click("link=%s" % title)
2323
self.assert_element("nav.documentation-nav")
2424
self.assert_text(title, "h1")
25-
self.highlight("div.description div.abstract")
25+
self.assert_text("Enable WebDriver and run a test.", "div.abstract")
2626
self.highlight("h2")
2727
h3 = "h3:nth-of-type(%s)"
2828
self.assert_text("Make Sure You Have Safari’s WebDriver", h3 % "1")

examples/test_error_page.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ def test_error_page(self):
1414
self.highlight('img[alt*="404"]')
1515
self.highlight("img#octobi_wan_catnobi")
1616
self.highlight("img#speeder")
17+
self.save_screenshot_after_test = True # Automatic if test fails

examples/tour_examples/maps_introjs_tour.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
from seleniumbase import BaseCase
23

34

@@ -9,7 +10,9 @@ def test_google_maps_tour(self):
910
self.wait_for_element("#zoom", timeout=20)
1011

1112
self.create_tour(theme="introjs")
12-
self.add_tour_step("Welcome to Google Maps", title="SeleniumBase Tour")
13+
self.add_tour_step(
14+
"Welcome to Google Maps", title="✅ SeleniumBase Tours 🌎"
15+
)
1316
self.add_tour_step(
1417
"The location goes here.", "#searchboxinput", title="Search Box"
1518
)
@@ -45,7 +48,8 @@ def test_google_maps_tour(self):
4548
alignment="left",
4649
)
4750
self.add_tour_step(
48-
"Thanks for using SeleniumBase Tours!", title="End of Guided Tour"
51+
"Thanks for using SeleniumBase Tours!",
52+
title="🚃 End of Guided Tour 🚃"
4953
)
5054
self.export_tour(filename="maps_introjs_tour.js")
5155
self.play_tour()

help_docs/customizing_test_runs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ pytest my_first_test.py --settings-file=custom_settings.py
164164
--devtools # (Open Chrome's DevTools when the browser opens.)
165165
--reuse-session | --rs # (Reuse the browser session between tests.)
166166
--crumbs # (Delete all cookies between tests reusing a session.)
167-
--maximize # (Start tests with the web browser window maximized.)
167+
--window-size # (Set the browser window size: "Width,Height".)
168+
--maximize # (Start tests with the browser window maximized.)
168169
--screenshot # (Save a screenshot at the end of each test.)
169170
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
170171
--external-pdf # (Set Chrome "plugins.always_open_pdf_externally": True.)

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "3.2.7"
2+
__version__ = "3.2.8"

seleniumbase/behave/behave_sb.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
-D devtools (Open Chrome's DevTools when the browser opens.)
7676
-D reuse-session | -D rs (Reuse browser session between tests.)
7777
-D crumbs (Delete all cookies between tests reusing a session.)
78-
-D maximize (Start tests with the web browser window maximized.)
78+
-D window-size (Set the browser window size: "Width,Height".)
79+
-D maximize (Start tests with the browser window maximized.)
7980
-D screenshot (Save a screenshot at the end of each test.)
8081
-D visual-baseline (Set the visual baseline for Visual/Layout tests.)
8182
-D external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@@ -160,6 +161,7 @@ def get_configured_sb(context):
160161
sb._reuse_session = False
161162
sb._crumbs = False
162163
sb.visual_baseline = False
164+
sb.window_size = None
163165
sb.maximize_option = False
164166
sb.save_screenshot_after_test = False
165167
sb.timeout_multiplier = None
@@ -469,6 +471,13 @@ def get_configured_sb(context):
469471
if low_key in ["visual-baseline", "visual_baseline"]:
470472
sb.visual_baseline = True
471473
continue
474+
# Handle: -D window-size=Width,Height / window_size=Width,Height
475+
if low_key in ["window-size", "window_size"]:
476+
window_size = userdata[key]
477+
if window_size == "true":
478+
window_size = sb.window_size # revert to default
479+
sb.window_size = window_size
480+
continue
472481
# Handle: -D maximize / fullscreen / maximize-window
473482
if low_key in [
474483
"maximize", "fullscreen", "maximize-window", "maximize_window"
@@ -693,13 +702,40 @@ def get_configured_sb(context):
693702
# If the port is "443", the protocol is "https"
694703
if str(sb.port) == "443":
695704
sb.protocol = "https"
705+
if sb.window_size:
706+
window_size = sb.window_size
707+
if window_size.count(",") != 1:
708+
message = (
709+
'\n\n window_size expects a "width,height" string!'
710+
'\n (Your input was: "%s")\n' % window_size
711+
)
712+
raise Exception(message)
713+
window_size = window_size.replace(" ", "")
714+
width = None
715+
height = None
716+
try:
717+
width = int(window_size.split(",")[0])
718+
height = int(window_size.split(",")[1])
719+
except Exception:
720+
message = (
721+
'\n\n Expecting integer values for "width,height"!'
722+
'\n (window_size input was: "%s")\n' % window_size
723+
)
724+
raise Exception(message)
725+
settings.CHROME_START_WIDTH = width
726+
settings.CHROME_START_HEIGHT = height
727+
settings.HEADLESS_START_WIDTH = width
728+
settings.HEADLESS_START_HEIGHT = height
696729

697730
# Set sb_config
698731
sb_config.browser = sb.browser
699732
sb_config.headless = sb.headless
700733
sb_config.headed = sb.headed
734+
sb_config.window_size = sb.window_size
735+
sb_config.maximize_option = sb.maximize_option
701736
sb_config.xvfb = sb.xvfb
702737
sb_config.save_screenshot = sb.save_screenshot_after_test
738+
sb_config._has_logs = False
703739
sb_config.variables = sb.variables
704740
sb_config.dashboard = sb.dashboard
705741
sb_config.dash_title = sb.dash_title
@@ -1035,7 +1071,11 @@ def _perform_behave_terminal_summary_():
10351071
if sb_config.dashboard:
10361072
# Print link a second time because the first one may be off-screen
10371073
print("%s- Dashboard:%s %s" % (c2, cr, dash_path))
1038-
if sb_config._has_exception or sb_config.save_screenshot:
1074+
if (
1075+
sb_config._has_exception
1076+
or sb_config.save_screenshot
1077+
or sb_config._has_logs
1078+
):
10391079
# Log files are generated during test failures and Screenshot Mode
10401080
print("%s--- LogPath:%s %s" % (c2, cr, latest_logs_dir))
10411081
if (

seleniumbase/fixtures/base_case.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,7 @@ def save_screenshot_to_logs(
32593259
origin = self.get_origin()
32603260
action = ["ss_tl", "", origin, time_stamp]
32613261
self.__extra_actions.append(action)
3262+
sb_config._has_logs = True
32623263
return page_actions.save_screenshot(self.driver, name, test_logpath)
32633264

32643265
def save_page_source(self, name, folder=None):
@@ -11970,6 +11971,31 @@ def setUp(self, masterqa_mode=False):
1197011971
self.extension_zip = sb_config.extension_zip
1197111972
self.extension_dir = sb_config.extension_dir
1197211973
self.external_pdf = sb_config.external_pdf
11974+
self.window_size = sb_config.window_size
11975+
window_size = self.window_size
11976+
if window_size:
11977+
if window_size.count(",") != 1:
11978+
message = (
11979+
'\n\n window_size expects a "width,height" string!'
11980+
'\n (Your input was: "%s")\n' % window_size
11981+
)
11982+
raise Exception(message)
11983+
window_size = window_size.replace(" ", "")
11984+
width = None
11985+
height = None
11986+
try:
11987+
width = int(window_size.split(",")[0])
11988+
height = int(window_size.split(",")[1])
11989+
except Exception:
11990+
message = (
11991+
'\n\n Expecting integer values for "width,height"!'
11992+
'\n (window_size input was: "%s")\n' % window_size
11993+
)
11994+
raise Exception(message)
11995+
settings.CHROME_START_WIDTH = width
11996+
settings.CHROME_START_HEIGHT = height
11997+
settings.HEADLESS_START_WIDTH = width
11998+
settings.HEADLESS_START_HEIGHT = height
1197311999
self.maximize_option = sb_config.maximize_option
1197412000
self.save_screenshot_after_test = sb_config.save_screenshot
1197512001
self.visual_baseline = sb_config.visual_baseline
@@ -12945,6 +12971,7 @@ def save_teardown_screenshot(self):
1294512971
self.__set_last_page_screenshot()
1294612972
self.__set_last_page_url()
1294712973
self.__set_last_page_source()
12974+
sb_config._has_logs = True
1294812975
if self.is_pytest:
1294912976
self.__add_pytest_html_extra()
1295012977

@@ -13025,6 +13052,7 @@ def tearDown(self):
1302513052
if has_exception:
1302613053
self.__add_pytest_html_extra()
1302713054
sb_config._has_exception = True
13055+
sb_config._has_logs = True
1302813056
if (
1302913057
self.with_testing_base
1303013058
and not has_exception
@@ -13042,6 +13070,7 @@ def tearDown(self):
1304213070
self.__last_page_screenshot_png,
1304313071
)
1304413072
self.__add_pytest_html_extra()
13073+
sb_config._has_logs = True
1304513074
if self.with_testing_base and has_exception:
1304613075
test_logpath = os.path.join(self.log_path, test_id)
1304713076
self.__create_log_path_as_needed(test_logpath)

seleniumbase/plugins/pytest_plugin.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def pytest_addoption(parser):
8686
--devtools (Open Chrome's DevTools when the browser opens.)
8787
--reuse-session | --rs (Reuse browser session between tests.)
8888
--crumbs (Delete all cookies between tests reusing a session.)
89-
--maximize (Start tests with the web browser window maximized.)
89+
--window-size (Set the browser window size: "Width,Height".)
90+
--maximize (Start tests with the browser window maximized.)
9091
--screenshot (Save a screenshot at the end of each test.)
9192
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
9293
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@@ -893,6 +894,17 @@ def pytest_addoption(parser):
893894
that reuse the same browser session. This option
894895
is only needed when using "--reuse-session".""",
895896
)
897+
parser.addoption(
898+
"--window-size",
899+
"--window_size",
900+
action="store",
901+
dest="window_size",
902+
default=None,
903+
help="""The option to set the default window "width,height".
904+
Format: A comma-separated string with the 2 values.
905+
Example: "1200,800"
906+
Default: None. (Will use default values if None)""",
907+
)
896908
parser.addoption(
897909
"--maximize_window",
898910
"--maximize-window",
@@ -901,8 +913,8 @@ def pytest_addoption(parser):
901913
action="store_true",
902914
dest="maximize_option",
903915
default=False,
904-
help="""The option to start with the browser window
905-
maximized.""",
916+
help="""The option to start with a maximized browser window.
917+
(Overrides the "window-size" option if used.)""",
906918
)
907919
parser.addoption(
908920
"--screenshot",
@@ -1190,12 +1202,14 @@ def pytest_configure(config):
11901202
sb_config.reuse_session = config.getoption("reuse_session")
11911203
sb_config.crumbs = config.getoption("crumbs")
11921204
sb_config.shared_driver = None # The default driver for session reuse
1205+
sb_config.window_size = config.getoption("window_size")
11931206
sb_config.maximize_option = config.getoption("maximize_option")
11941207
sb_config.save_screenshot = config.getoption("save_screenshot")
11951208
sb_config.visual_baseline = config.getoption("visual_baseline")
11961209
sb_config.external_pdf = config.getoption("external_pdf")
11971210
sb_config.timeout_multiplier = config.getoption("timeout_multiplier")
11981211
sb_config._is_timeout_changed = False
1212+
sb_config._has_logs = False
11991213
sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT
12001214
sb_config._LARGE_TIMEOUT = settings.LARGE_TIMEOUT
12011215
sb_config.pytest_html_report = config.getoption("htmlpath") # --html=FILE
@@ -1461,7 +1475,11 @@ def pytest_terminal_summary(terminalreporter):
14611475
# Print link a second time because the first one may be off-screen
14621476
dashboard_file = os.getcwd() + "/dashboard.html"
14631477
terminalreporter.write_sep("-", "Dashboard: %s" % dashboard_file)
1464-
if sb_config._has_exception or sb_config.save_screenshot:
1478+
if (
1479+
sb_config._has_exception
1480+
or sb_config.save_screenshot
1481+
or sb_config._has_logs
1482+
):
14651483
# Log files are generated during test failures and Screenshot Mode
14661484
terminalreporter.write_sep("-", "LogPath: %s" % latest_logs_dir)
14671485

0 commit comments

Comments
 (0)