Skip to content

Commit 547ae19

Browse files
committed
Add option to set the window_size: "Width,Height"
1 parent cf59d53 commit 547ae19

File tree

7 files changed

+122
-8
lines changed

7 files changed

+122
-8
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

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/behave/behave_sb.py

Lines changed: 36 additions & 1 deletion
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,11 +702,37 @@ 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
703738
sb_config.variables = sb.variables

seleniumbase/fixtures/base_case.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11970,6 +11970,31 @@ def setUp(self, masterqa_mode=False):
1197011970
self.extension_zip = sb_config.extension_zip
1197111971
self.extension_dir = sb_config.extension_dir
1197211972
self.external_pdf = sb_config.external_pdf
11973+
self.window_size = sb_config.window_size
11974+
window_size = self.window_size
11975+
if window_size:
11976+
if window_size.count(",") != 1:
11977+
message = (
11978+
'\n\n window_size expects a "width,height" string!'
11979+
'\n (Your input was: "%s")\n' % window_size
11980+
)
11981+
raise Exception(message)
11982+
window_size = window_size.replace(" ", "")
11983+
width = None
11984+
height = None
11985+
try:
11986+
width = int(window_size.split(",")[0])
11987+
height = int(window_size.split(",")[1])
11988+
except Exception:
11989+
message = (
11990+
'\n\n Expecting integer values for "width,height"!'
11991+
'\n (window_size input was: "%s")\n' % window_size
11992+
)
11993+
raise Exception(message)
11994+
settings.CHROME_START_WIDTH = width
11995+
settings.CHROME_START_HEIGHT = height
11996+
settings.HEADLESS_START_WIDTH = width
11997+
settings.HEADLESS_START_HEIGHT = height
1197311998
self.maximize_option = sb_config.maximize_option
1197411999
self.save_screenshot_after_test = sb_config.save_screenshot
1197512000
self.visual_baseline = sb_config.visual_baseline

seleniumbase/plugins/pytest_plugin.py

Lines changed: 16 additions & 3 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,6 +1202,7 @@ 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")

seleniumbase/plugins/selenium_plugin.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class SeleniumBrowser(Plugin):
6363
--incognito (Enable Chrome's Incognito mode.)
6464
--guest (Enable Chrome's Guest mode.)
6565
--devtools (Open Chrome's DevTools when the browser opens.)
66-
--maximize (Start tests with the web browser window maximized.)
66+
--window-size (Set the browser window size: "Width,Height".)
67+
--maximize (Start tests with the browser window maximized.)
6768
--screenshot (Save a screenshot at the end of each test.)
6869
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
6970
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@@ -596,6 +597,17 @@ def options(self, parser, env):
596597
default=False,
597598
help="""Using this opens Chrome's DevTools.""",
598599
)
600+
parser.add_option(
601+
"--window-size",
602+
"--window_size",
603+
action="store",
604+
dest="window_size",
605+
default=None,
606+
help="""The option to set the default window "width,height".
607+
Format: A comma-separated string with the 2 values.
608+
Example: "1200,800"
609+
Default: None. (Will use default values if None)""",
610+
)
599611
parser.add_option(
600612
"--maximize_window",
601613
"--maximize-window",
@@ -604,7 +616,8 @@ def options(self, parser, env):
604616
action="store_true",
605617
dest="maximize_option",
606618
default=False,
607-
help="""The option to start with the web browser maximized.""",
619+
help="""The option to start with a maximized browser window.
620+
(Overrides the "window-size" option if used.)""",
608621
)
609622
parser.add_option(
610623
"--screenshot",
@@ -665,6 +678,30 @@ def beforeTest(self, test):
665678
'\n (Your browser choice was: "%s")\n' % browser
666679
)
667680
raise Exception(message)
681+
window_size = self.options.window_size
682+
if window_size:
683+
if window_size.count(",") != 1:
684+
message = (
685+
'\n\n window_size expects a "width,height" string!'
686+
'\n (Your input was: "%s")\n' % window_size
687+
)
688+
raise Exception(message)
689+
window_size = window_size.replace(" ", "")
690+
width = None
691+
height = None
692+
try:
693+
width = int(window_size.split(",")[0])
694+
height = int(window_size.split(",")[1])
695+
except Exception:
696+
message = (
697+
'\n\n Expecting integer values for "width,height"!'
698+
'\n (window_size input was: "%s")\n' % window_size
699+
)
700+
raise Exception(message)
701+
settings.CHROME_START_WIDTH = width
702+
settings.CHROME_START_HEIGHT = height
703+
settings.HEADLESS_START_WIDTH = width
704+
settings.HEADLESS_START_HEIGHT = height
668705
test.test.is_nosetest = True
669706
test.test.browser = self.options.browser
670707
test.test.cap_file = self.options.cap_file
@@ -720,6 +757,7 @@ def beforeTest(self, test):
720757
test.test.incognito = self.options.incognito
721758
test.test.guest_mode = self.options.guest_mode
722759
test.test.devtools = self.options.devtools
760+
test.test.window_size = self.options.window_size
723761
test.test.maximize_option = self.options.maximize_option
724762
test.test.save_screenshot_after_test = self.options.save_screenshot
725763
test.test.visual_baseline = self.options.visual_baseline

0 commit comments

Comments
 (0)