From 547ae19489fea8040e2e2dc962a3477c731fd83a Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Mon, 6 Jun 2022 23:19:13 -0400 Subject: [PATCH 1/5] Add option to set the window_size: "Width,Height" --- README.md | 3 +- examples/raw_parameter_script.py | 1 + help_docs/customizing_test_runs.md | 3 +- seleniumbase/behave/behave_sb.py | 37 +++++++++++++++++++++- seleniumbase/fixtures/base_case.py | 25 +++++++++++++++ seleniumbase/plugins/pytest_plugin.py | 19 +++++++++-- seleniumbase/plugins/selenium_plugin.py | 42 +++++++++++++++++++++++-- 7 files changed, 122 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1ebc6d73..340c965f 100755 --- a/README.md +++ b/README.md @@ -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.) diff --git a/examples/raw_parameter_script.py b/examples/raw_parameter_script.py index 31e1a599..131b68f5 100755 --- a/examples/raw_parameter_script.py +++ b/examples/raw_parameter_script.py @@ -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 diff --git a/help_docs/customizing_test_runs.md b/help_docs/customizing_test_runs.md index 15f09c6d..060d795d 100755 --- a/help_docs/customizing_test_runs.md +++ b/help_docs/customizing_test_runs.md @@ -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.) diff --git a/seleniumbase/behave/behave_sb.py b/seleniumbase/behave/behave_sb.py index 413bd76a..63d7b276 100644 --- a/seleniumbase/behave/behave_sb.py +++ b/seleniumbase/behave/behave_sb.py @@ -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 diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index 120f8386..d10f3f5f 100755 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -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 diff --git a/seleniumbase/plugins/pytest_plugin.py b/seleniumbase/plugins/pytest_plugin.py index 821e5327..c4d575b1 100644 --- a/seleniumbase/plugins/pytest_plugin.py +++ b/seleniumbase/plugins/pytest_plugin.py @@ -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") diff --git a/seleniumbase/plugins/selenium_plugin.py b/seleniumbase/plugins/selenium_plugin.py index ffd54a26..5724ddfa 100755 --- a/seleniumbase/plugins/selenium_plugin.py +++ b/seleniumbase/plugins/selenium_plugin.py @@ -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 From 9f4b412e7997fa772129733af440cc28d53b71e4 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Mon, 6 Jun 2022 23:50:01 -0400 Subject: [PATCH 2/5] Display latest_logs link if files were saved to the logs --- seleniumbase/behave/behave_sb.py | 7 ++++++- seleniumbase/fixtures/base_case.py | 4 ++++ seleniumbase/plugins/pytest_plugin.py | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/seleniumbase/behave/behave_sb.py b/seleniumbase/behave/behave_sb.py index 63d7b276..a9fda8bb 100644 --- a/seleniumbase/behave/behave_sb.py +++ b/seleniumbase/behave/behave_sb.py @@ -735,6 +735,7 @@ def get_configured_sb(context): sb_config.maximize_option = sb.maximize_option sb_config.xvfb = sb.xvfb sb_config.save_screenshot = sb.save_screenshot_after_test + sb_config._has_logs = False sb_config.variables = sb.variables sb_config.dashboard = sb.dashboard sb_config.dash_title = sb.dash_title @@ -1070,7 +1071,11 @@ def _perform_behave_terminal_summary_(): if sb_config.dashboard: # Print link a second time because the first one may be off-screen print("%s- Dashboard:%s %s" % (c2, cr, dash_path)) - if sb_config._has_exception or sb_config.save_screenshot: + if ( + sb_config._has_exception + or sb_config.save_screenshot + or sb_config._has_logs + ): # Log files are generated during test failures and Screenshot Mode print("%s--- LogPath:%s %s" % (c2, cr, latest_logs_dir)) if ( diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index d10f3f5f..ab7ffea6 100755 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -3259,6 +3259,7 @@ class BaseCase(unittest.TestCase): origin = self.get_origin() action = ["ss_tl", "", origin, time_stamp] self.__extra_actions.append(action) + sb_config._has_logs = True return page_actions.save_screenshot(self.driver, name, test_logpath) def save_page_source(self, name, folder=None): @@ -12970,6 +12971,7 @@ class BaseCase(unittest.TestCase): self.__set_last_page_screenshot() self.__set_last_page_url() self.__set_last_page_source() + sb_config._has_logs = True if self.is_pytest: self.__add_pytest_html_extra() @@ -13050,6 +13052,7 @@ class BaseCase(unittest.TestCase): if has_exception: self.__add_pytest_html_extra() sb_config._has_exception = True + sb_config._has_logs = True if ( self.with_testing_base and not has_exception @@ -13067,6 +13070,7 @@ class BaseCase(unittest.TestCase): self.__last_page_screenshot_png, ) self.__add_pytest_html_extra() + sb_config._has_logs = True if self.with_testing_base and has_exception: test_logpath = os.path.join(self.log_path, test_id) self.__create_log_path_as_needed(test_logpath) diff --git a/seleniumbase/plugins/pytest_plugin.py b/seleniumbase/plugins/pytest_plugin.py index c4d575b1..77e85362 100644 --- a/seleniumbase/plugins/pytest_plugin.py +++ b/seleniumbase/plugins/pytest_plugin.py @@ -1209,6 +1209,7 @@ def pytest_configure(config): sb_config.external_pdf = config.getoption("external_pdf") sb_config.timeout_multiplier = config.getoption("timeout_multiplier") sb_config._is_timeout_changed = False + sb_config._has_logs = False sb_config._SMALL_TIMEOUT = settings.SMALL_TIMEOUT sb_config._LARGE_TIMEOUT = settings.LARGE_TIMEOUT sb_config.pytest_html_report = config.getoption("htmlpath") # --html=FILE @@ -1474,7 +1475,11 @@ def pytest_terminal_summary(terminalreporter): # Print link a second time because the first one may be off-screen dashboard_file = os.getcwd() + "/dashboard.html" terminalreporter.write_sep("-", "Dashboard: %s" % dashboard_file) - if sb_config._has_exception or sb_config.save_screenshot: + if ( + sb_config._has_exception + or sb_config.save_screenshot + or sb_config._has_logs + ): # Log files are generated during test failures and Screenshot Mode terminalreporter.write_sep("-", "LogPath: %s" % latest_logs_dir) From 93a0e9593724925ceadacf02c254e97db4a863c8 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Mon, 6 Jun 2022 23:50:26 -0400 Subject: [PATCH 3/5] Update examples --- examples/test_error_page.py | 1 + examples/tour_examples/maps_introjs_tour.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/test_error_page.py b/examples/test_error_page.py index 3d9e1093..109f0c21 100644 --- a/examples/test_error_page.py +++ b/examples/test_error_page.py @@ -14,3 +14,4 @@ class ErrorPageTests(BaseCase): self.highlight('img[alt*="404"]') self.highlight("img#octobi_wan_catnobi") self.highlight("img#speeder") + self.save_screenshot_after_test = True # Automatic if test fails diff --git a/examples/tour_examples/maps_introjs_tour.py b/examples/tour_examples/maps_introjs_tour.py index 60612a0d..fb30314e 100755 --- a/examples/tour_examples/maps_introjs_tour.py +++ b/examples/tour_examples/maps_introjs_tour.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from seleniumbase import BaseCase @@ -9,7 +10,9 @@ class MyTourClass(BaseCase): self.wait_for_element("#zoom", timeout=20) self.create_tour(theme="introjs") - self.add_tour_step("Welcome to Google Maps", title="SeleniumBase Tour") + self.add_tour_step( + "Welcome to Google Maps", title="βœ… SeleniumBase Tours 🌎" + ) self.add_tour_step( "The location goes here.", "#searchboxinput", title="Search Box" ) @@ -45,7 +48,8 @@ class MyTourClass(BaseCase): alignment="left", ) self.add_tour_step( - "Thanks for using SeleniumBase Tours!", title="End of Guided Tour" + "Thanks for using SeleniumBase Tours!", + title="πŸšƒ End of Guided Tour πŸšƒ" ) self.export_tour(filename="maps_introjs_tour.js") self.play_tour() From 9187625e64a130709b1d9dc65baaee36371d8fde Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Tue, 7 Jun 2022 00:14:51 -0400 Subject: [PATCH 4/5] Version 3.2.8 --- seleniumbase/__version__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seleniumbase/__version__.py b/seleniumbase/__version__.py index 4da6beef..852e0204 100755 --- a/seleniumbase/__version__.py +++ b/seleniumbase/__version__.py @@ -1,2 +1,2 @@ # seleniumbase package -__version__ = "3.2.7" +__version__ = "3.2.8" From 3c38af374f102efcca4dabc106befdda60c8a4d4 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Tue, 7 Jun 2022 00:53:10 -0400 Subject: [PATCH 5/5] Update an example --- examples/test_apple_site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/test_apple_site.py b/examples/test_apple_site.py index 121083e0..33a7664c 100755 --- a/examples/test_apple_site.py +++ b/examples/test_apple_site.py @@ -22,7 +22,7 @@ class AppleTests(BaseCase): self.click("link=%s" % title) self.assert_element("nav.documentation-nav") self.assert_text(title, "h1") - self.highlight("div.description div.abstract") + self.assert_text("Enable WebDriver and run a test.", "div.abstract") self.highlight("h2") h3 = "h3:nth-of-type(%s)" self.assert_text("Make Sure You Have Safari’s WebDriver", h3 % "1")