Add option to set the window_size: "Width,Height"

This commit is contained in:
Michael Mintz 2022-06-06 23:19:13 -04:00
parent cf59d53410
commit 547ae19489
7 changed files with 122 additions and 8 deletions

View File

@ -489,7 +489,8 @@ The code above will leave your browser window open in case there's a failure. (i
--devtools # (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs # (Reuse the browser session between tests.)
--crumbs # (Delete all cookies between tests reusing a session.)
--maximize # (Start tests with the web browser window maximized.)
--window-size # (Set the browser window size: "Width,Height".)
--maximize # (Start tests with the browser window maximized.)
--screenshot # (Save a screenshot at the end of each test.)
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
--external-pdf # (Set Chrome "plugins.always_open_pdf_externally": True.)

View File

@ -69,6 +69,7 @@ if pure_python:
sb._reuse_session = False
sb._crumbs = False
sb.visual_baseline = False
sb.window_size = None
sb.maximize_option = False
sb.save_screenshot_after_test = False
sb.timeout_multiplier = None

View File

@ -164,7 +164,8 @@ pytest my_first_test.py --settings-file=custom_settings.py
--devtools # (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs # (Reuse the browser session between tests.)
--crumbs # (Delete all cookies between tests reusing a session.)
--maximize # (Start tests with the web browser window maximized.)
--window-size # (Set the browser window size: "Width,Height".)
--maximize # (Start tests with the browser window maximized.)
--screenshot # (Save a screenshot at the end of each test.)
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
--external-pdf # (Set Chrome "plugins.always_open_pdf_externally": True.)

View File

@ -75,7 +75,8 @@ behave -D agent="User Agent String" -D demo
-D devtools (Open Chrome's DevTools when the browser opens.)
-D reuse-session | -D rs (Reuse browser session between tests.)
-D crumbs (Delete all cookies between tests reusing a session.)
-D maximize (Start tests with the web browser window maximized.)
-D window-size (Set the browser window size: "Width,Height".)
-D maximize (Start tests with the browser window maximized.)
-D screenshot (Save a screenshot at the end of each test.)
-D visual-baseline (Set the visual baseline for Visual/Layout tests.)
-D external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@ -160,6 +161,7 @@ def get_configured_sb(context):
sb._reuse_session = False
sb._crumbs = False
sb.visual_baseline = False
sb.window_size = None
sb.maximize_option = False
sb.save_screenshot_after_test = False
sb.timeout_multiplier = None
@ -469,6 +471,13 @@ def get_configured_sb(context):
if low_key in ["visual-baseline", "visual_baseline"]:
sb.visual_baseline = True
continue
# Handle: -D window-size=Width,Height / window_size=Width,Height
if low_key in ["window-size", "window_size"]:
window_size = userdata[key]
if window_size == "true":
window_size = sb.window_size # revert to default
sb.window_size = window_size
continue
# Handle: -D maximize / fullscreen / maximize-window
if low_key in [
"maximize", "fullscreen", "maximize-window", "maximize_window"
@ -693,11 +702,37 @@ def get_configured_sb(context):
# If the port is "443", the protocol is "https"
if str(sb.port) == "443":
sb.protocol = "https"
if sb.window_size:
window_size = sb.window_size
if window_size.count(",") != 1:
message = (
'\n\n window_size expects a "width,height" string!'
'\n (Your input was: "%s")\n' % window_size
)
raise Exception(message)
window_size = window_size.replace(" ", "")
width = None
height = None
try:
width = int(window_size.split(",")[0])
height = int(window_size.split(",")[1])
except Exception:
message = (
'\n\n Expecting integer values for "width,height"!'
'\n (window_size input was: "%s")\n' % window_size
)
raise Exception(message)
settings.CHROME_START_WIDTH = width
settings.CHROME_START_HEIGHT = height
settings.HEADLESS_START_WIDTH = width
settings.HEADLESS_START_HEIGHT = height
# Set sb_config
sb_config.browser = sb.browser
sb_config.headless = sb.headless
sb_config.headed = sb.headed
sb_config.window_size = sb.window_size
sb_config.maximize_option = sb.maximize_option
sb_config.xvfb = sb.xvfb
sb_config.save_screenshot = sb.save_screenshot_after_test
sb_config.variables = sb.variables

View File

@ -11970,6 +11970,31 @@ class BaseCase(unittest.TestCase):
self.extension_zip = sb_config.extension_zip
self.extension_dir = sb_config.extension_dir
self.external_pdf = sb_config.external_pdf
self.window_size = sb_config.window_size
window_size = self.window_size
if window_size:
if window_size.count(",") != 1:
message = (
'\n\n window_size expects a "width,height" string!'
'\n (Your input was: "%s")\n' % window_size
)
raise Exception(message)
window_size = window_size.replace(" ", "")
width = None
height = None
try:
width = int(window_size.split(",")[0])
height = int(window_size.split(",")[1])
except Exception:
message = (
'\n\n Expecting integer values for "width,height"!'
'\n (window_size input was: "%s")\n' % window_size
)
raise Exception(message)
settings.CHROME_START_WIDTH = width
settings.CHROME_START_HEIGHT = height
settings.HEADLESS_START_WIDTH = width
settings.HEADLESS_START_HEIGHT = height
self.maximize_option = sb_config.maximize_option
self.save_screenshot_after_test = sb_config.save_screenshot
self.visual_baseline = sb_config.visual_baseline

View File

@ -86,7 +86,8 @@ def pytest_addoption(parser):
--devtools (Open Chrome's DevTools when the browser opens.)
--reuse-session | --rs (Reuse browser session between tests.)
--crumbs (Delete all cookies between tests reusing a session.)
--maximize (Start tests with the web browser window maximized.)
--window-size (Set the browser window size: "Width,Height".)
--maximize (Start tests with the browser window maximized.)
--screenshot (Save a screenshot at the end of each test.)
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@ -893,6 +894,17 @@ def pytest_addoption(parser):
that reuse the same browser session. This option
is only needed when using "--reuse-session".""",
)
parser.addoption(
"--window-size",
"--window_size",
action="store",
dest="window_size",
default=None,
help="""The option to set the default window "width,height".
Format: A comma-separated string with the 2 values.
Example: "1200,800"
Default: None. (Will use default values if None)""",
)
parser.addoption(
"--maximize_window",
"--maximize-window",
@ -901,8 +913,8 @@ def pytest_addoption(parser):
action="store_true",
dest="maximize_option",
default=False,
help="""The option to start with the browser window
maximized.""",
help="""The option to start with a maximized browser window.
(Overrides the "window-size" option if used.)""",
)
parser.addoption(
"--screenshot",
@ -1190,6 +1202,7 @@ def pytest_configure(config):
sb_config.reuse_session = config.getoption("reuse_session")
sb_config.crumbs = config.getoption("crumbs")
sb_config.shared_driver = None # The default driver for session reuse
sb_config.window_size = config.getoption("window_size")
sb_config.maximize_option = config.getoption("maximize_option")
sb_config.save_screenshot = config.getoption("save_screenshot")
sb_config.visual_baseline = config.getoption("visual_baseline")

View File

@ -63,7 +63,8 @@ class SeleniumBrowser(Plugin):
--incognito (Enable Chrome's Incognito mode.)
--guest (Enable Chrome's Guest mode.)
--devtools (Open Chrome's DevTools when the browser opens.)
--maximize (Start tests with the web browser window maximized.)
--window-size (Set the browser window size: "Width,Height".)
--maximize (Start tests with the browser window maximized.)
--screenshot (Save a screenshot at the end of each test.)
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
--external-pdf (Set Chromium "plugins.always_open_pdf_externally": True.)
@ -596,6 +597,17 @@ class SeleniumBrowser(Plugin):
default=False,
help="""Using this opens Chrome's DevTools.""",
)
parser.add_option(
"--window-size",
"--window_size",
action="store",
dest="window_size",
default=None,
help="""The option to set the default window "width,height".
Format: A comma-separated string with the 2 values.
Example: "1200,800"
Default: None. (Will use default values if None)""",
)
parser.add_option(
"--maximize_window",
"--maximize-window",
@ -604,7 +616,8 @@ class SeleniumBrowser(Plugin):
action="store_true",
dest="maximize_option",
default=False,
help="""The option to start with the web browser maximized.""",
help="""The option to start with a maximized browser window.
(Overrides the "window-size" option if used.)""",
)
parser.add_option(
"--screenshot",
@ -665,6 +678,30 @@ class SeleniumBrowser(Plugin):
'\n (Your browser choice was: "%s")\n' % browser
)
raise Exception(message)
window_size = self.options.window_size
if window_size:
if window_size.count(",") != 1:
message = (
'\n\n window_size expects a "width,height" string!'
'\n (Your input was: "%s")\n' % window_size
)
raise Exception(message)
window_size = window_size.replace(" ", "")
width = None
height = None
try:
width = int(window_size.split(",")[0])
height = int(window_size.split(",")[1])
except Exception:
message = (
'\n\n Expecting integer values for "width,height"!'
'\n (window_size input was: "%s")\n' % window_size
)
raise Exception(message)
settings.CHROME_START_WIDTH = width
settings.CHROME_START_HEIGHT = height
settings.HEADLESS_START_WIDTH = width
settings.HEADLESS_START_HEIGHT = height
test.test.is_nosetest = True
test.test.browser = self.options.browser
test.test.cap_file = self.options.cap_file
@ -720,6 +757,7 @@ class SeleniumBrowser(Plugin):
test.test.incognito = self.options.incognito
test.test.guest_mode = self.options.guest_mode
test.test.devtools = self.options.devtools
test.test.window_size = self.options.window_size
test.test.maximize_option = self.options.maximize_option
test.test.save_screenshot_after_test = self.options.save_screenshot
test.test.visual_baseline = self.options.visual_baseline