Skip to content

Commit cb9a2d2

Browse files
authored
Merge pull request #290 from seleniumbase/screenshots-after-tests
Add the ability to save screenshots at the end of tests
2 parents 6d5769b + 93066fc commit cb9a2d2

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,7 @@ def setUp(self):
28012801
self.js_checking_on = sb_config.js_checking_on
28022802
self.ad_block_on = sb_config.ad_block_on
28032803
self.verify_delay = sb_config.verify_delay
2804+
self.save_screenshot_after_test = sb_config.save_screenshot
28042805
self.timeout_multiplier = sb_config.timeout_multiplier
28052806
self.use_grid = False
28062807
if self.servername != "localhost":
@@ -2981,6 +2982,12 @@ def tearDown(self):
29812982
# Save a screenshot if logging is on when an exception occurs
29822983
if has_exception:
29832984
self.__add_pytest_html_extra()
2985+
if self.with_testing_base and not has_exception and (
2986+
self.save_screenshot_after_test):
2987+
test_logpath = self.log_path + "/" + test_id
2988+
if not os.path.exists(test_logpath):
2989+
os.makedirs(test_logpath)
2990+
log_helper.log_screenshot(test_logpath, self.driver)
29842991
if self.with_testing_base and has_exception:
29852992
test_logpath = self.log_path + "/" + test_id
29862993
if not os.path.exists(test_logpath):
@@ -3056,5 +3063,13 @@ def tearDown(self):
30563063
if len(self._drivers_list) > 0:
30573064
log_helper.log_screenshot(test_logpath, self.driver)
30583065
log_helper.log_page_source(test_logpath, self.driver)
3066+
elif self.save_screenshot_after_test:
3067+
test_id = "%s.%s.%s" % (self.__class__.__module__,
3068+
self.__class__.__name__,
3069+
self._testMethodName)
3070+
test_logpath = "latest_logs/" + test_id
3071+
if not os.path.exists(test_logpath):
3072+
os.makedirs(test_logpath)
3073+
log_helper.log_screenshot(test_logpath, self.driver)
30593074
# Finally close all open browser windows
30603075
self.__quit_all_drivers()

seleniumbase/plugins/pytest_plugin.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def pytest_addoption(parser):
4747
parser.addoption('--with-testing_base', action="store_true",
4848
dest='with_testing_base',
4949
default=True,
50-
help="Use to save logs (screenshots) when tests fail.")
50+
help="""Use to save logs and screenshots when tests fail.
51+
It's no longer needed to add the following arguments:
52+
--with-screen_shots
53+
--with-basic_test_info
54+
--with-page_source
55+
(Those modes are all active by default now when
56+
--with-testing_base is active. (Default: active)""")
5157
parser.addoption('--log_path', dest='log_path',
5258
default='latest_logs/',
5359
help='Where the log files are saved.')
@@ -70,15 +76,18 @@ def pytest_addoption(parser):
7076
parser.addoption('--with-screen_shots', action="store_true",
7177
dest='with_screen_shots',
7278
default=False,
73-
help="Use to save screenshots on test failure.")
79+
help="""Use to save screenshots on test failure.
80+
(When "--with-testing_base" is True, this is on.)""")
7481
parser.addoption('--with-basic_test_info', action="store_true",
7582
dest='with_basic_test_info',
7683
default=False,
77-
help="Use to save basic test info on test failure.")
84+
help="""Use to save basic test info on test failure.
85+
(When "--with-testing_base" is True, this is on.)""")
7886
parser.addoption('--with-page_source', action="store_true",
7987
dest='with_page_source',
8088
default=False,
81-
help="Use to save page source on test failure.")
89+
help="""Use to save page source on test failure.
90+
(When "--with-testing_base" is True, this is on.)""")
8291
parser.addoption('--server', action='store',
8392
dest='servername',
8493
default='localhost',
@@ -146,6 +155,11 @@ def pytest_addoption(parser):
146155
default=None,
147156
help="""Setting this overrides the default wait time
148157
before each MasterQA verification pop-up.""")
158+
parser.addoption('--save_screenshot', action='store_true',
159+
dest='save_screenshot',
160+
default=False,
161+
help="""Take a screenshot on last page after the last step
162+
of the test. (Added to the "latest_logs" folder.)""")
149163
parser.addoption('--timeout_multiplier', action='store',
150164
dest='timeout_multiplier',
151165
default=None,
@@ -182,6 +196,7 @@ def pytest_configure(config):
182196
sb_config.js_checking_on = config.getoption('js_checking_on')
183197
sb_config.ad_block_on = config.getoption('ad_block_on')
184198
sb_config.verify_delay = config.getoption('verify_delay')
199+
sb_config.save_screenshot = config.getoption('save_screenshot')
185200
sb_config.timeout_multiplier = config.getoption('timeout_multiplier')
186201

187202
if sb_config.with_testing_base:

seleniumbase/plugins/selenium_plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SeleniumBrowser(Plugin):
2828
self.options.js_checking_on -- option to check for js errors (--check_js)
2929
self.options.ad_block -- the option to block some display ads (--ad_block)
3030
self.options.verify_delay -- delay before MasterQA checks (--verify_delay)
31+
self.options.save_screenshot -- save screen after test (--save_screenshot)
3132
self.options.timeout_multiplier -- increase defaults (--timeout_multiplier)
3233
"""
3334
name = 'selenium' # Usage: --with-selenium
@@ -126,6 +127,12 @@ def options(self, parser, env):
126127
dest='verify_delay', default=None,
127128
help="""Setting this overrides the default wait time
128129
before each MasterQA verification pop-up.""")
130+
parser.add_option(
131+
'--save_screenshot', action="store_true",
132+
dest='save_screenshot',
133+
default=False,
134+
help="""Take a screenshot on last page after the last step
135+
of the test. (Added to the "latest_logs" folder.)""")
129136
parser.add_option(
130137
'--timeout_multiplier', action='store',
131138
dest='timeout_multiplier',
@@ -156,6 +163,7 @@ def beforeTest(self, test):
156163
test.test.js_checking_on = self.options.js_checking_on
157164
test.test.ad_block_on = self.options.ad_block_on
158165
test.test.verify_delay = self.options.verify_delay # MasterQA
166+
test.test.save_screenshot_after_test = self.options.save_screenshot
159167
test.test.timeout_multiplier = self.options.timeout_multiplier
160168
test.test.use_grid = False
161169
if test.test.servername != "localhost":

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.21.1',
20+
version='1.21.2',
2121
description='Reliable Browser Automation & Testing Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)