Skip to content

Commit 310b848

Browse files
committed
Add "--no-screenshot" option to skip saving screenshots
1 parent 17a9082 commit 310b848

File tree

5 files changed

+105
-28
lines changed

5 files changed

+105
-28
lines changed

seleniumbase/core/log_helper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
def log_screenshot(test_logpath, driver, screenshot=None, get=False):
1313
screenshot_name = settings.SCREENSHOT_NAME
1414
screenshot_path = os.path.join(test_logpath, screenshot_name)
15+
screenshot_skipped = constants.Warnings.SCREENSHOT_SKIPPED
1516
screenshot_warning = constants.Warnings.SCREENSHOT_UNDEFINED
17+
if (
18+
(hasattr(sb_config, "no_screenshot") and sb_config.no_screenshot)
19+
or screenshot == screenshot_skipped
20+
):
21+
if get:
22+
return screenshot
23+
return
1624
try:
1725
if not screenshot:
1826
element = driver.find_element_by_tag_name("body")

seleniumbase/fixtures/base_case.py

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12925,6 +12925,7 @@ def setUp(self, masterqa_mode=False):
1292512925
settings.HEADLESS_START_HEIGHT = height
1292612926
self.maximize_option = sb_config.maximize_option
1292712927
self.save_screenshot_after_test = sb_config.save_screenshot
12928+
self.no_screenshot_after_test = sb_config.no_screenshot
1292812929
self.visual_baseline = sb_config.visual_baseline
1292912930
self.timeout_multiplier = sb_config.timeout_multiplier
1293012931
self.pytest_html_report = sb_config.pytest_html_report
@@ -13226,38 +13227,73 @@ def setUp(self, masterqa_mode=False):
1322613227
def __set_last_page_screenshot(self):
1322713228
"""self.__last_page_screenshot is only for pytest html report logs.
1322813229
self.__last_page_screenshot_png is for all screenshot log files."""
13229-
if not self.__last_page_screenshot and (
13230-
not self.__last_page_screenshot_png
13230+
SCREENSHOT_SKIPPED = constants.Warnings.SCREENSHOT_SKIPPED
13231+
SCREENSHOT_UNDEFINED = constants.Warnings.SCREENSHOT_UNDEFINED
13232+
if (
13233+
hasattr(self, "no_screenshot_after_test")
13234+
and self.no_screenshot_after_test
13235+
):
13236+
from seleniumbase.core import encoded_images
13237+
13238+
NO_SCREENSHOT = encoded_images.get_no_screenshot_png()
13239+
self.__last_page_screenshot = NO_SCREENSHOT
13240+
self.__last_page_screenshot_png = SCREENSHOT_SKIPPED
13241+
sb_config._last_page_screenshot_png = NO_SCREENSHOT
13242+
return
13243+
element = None
13244+
if (
13245+
not self.__last_page_screenshot
13246+
and not self.__last_page_screenshot_png
1323113247
):
1323213248
try:
1323313249
element = self.driver.find_element(by="tag name", value="body")
13234-
if self.is_pytest and self.report_on:
13235-
self.__last_page_screenshot_png = (
13236-
self.driver.get_screenshot_as_png()
13237-
)
13250+
try:
1323813251
self.__last_page_screenshot = element.screenshot_as_base64
13239-
else:
13240-
self.__last_page_screenshot_png = element.screenshot_as_png
13241-
except Exception:
13242-
if not self.__last_page_screenshot:
13243-
if self.is_pytest and self.report_on:
13244-
try:
13245-
self.__last_page_screenshot = (
13246-
self.driver.get_screenshot_as_base64()
13247-
)
13248-
except Exception:
13249-
self.__last_page_screenshot = (
13250-
constants.Warnings.SCREENSHOT_UNDEFINED
13251-
)
13252-
if not self.__last_page_screenshot_png:
13252+
except Exception:
1325313253
try:
13254-
self.__last_page_screenshot_png = (
13255-
self.driver.get_screenshot_as_png()
13254+
self.__last_page_screenshot = (
13255+
self.driver.get_screenshot_as_base64()
1325613256
)
1325713257
except Exception:
13258+
pass
13259+
except Exception:
13260+
pass
13261+
if not self.__last_page_screenshot:
13262+
self.__last_page_screenshot = SCREENSHOT_UNDEFINED
13263+
self.__last_page_screenshot_png = SCREENSHOT_UNDEFINED
13264+
if element:
13265+
try:
1325813266
self.__last_page_screenshot_png = (
13259-
constants.Warnings.SCREENSHOT_UNDEFINED
13267+
element.screenshot_as_png
1326013268
)
13269+
except Exception:
13270+
try:
13271+
self.__last_page_screenshot_png = (
13272+
self.driver.get_screenshot_as_png()
13273+
)
13274+
except Exception:
13275+
pass
13276+
else:
13277+
import base64
13278+
13279+
try:
13280+
self.__last_page_screenshot_png = (
13281+
base64.b64decode(self.__last_page_screenshot)
13282+
)
13283+
except Exception:
13284+
if element:
13285+
try:
13286+
self.__last_page_screenshot_png = (
13287+
element.screenshot_as_png
13288+
)
13289+
except Exception:
13290+
try:
13291+
self.__last_page_screenshot_png = (
13292+
self.driver.get_screenshot_as_png()
13293+
)
13294+
except Exception:
13295+
pass
13296+
sb_config._last_page_screenshot_png = self.__last_page_screenshot_png
1326113297

1326213298
def __set_last_page_url(self):
1326313299
if not self.__last_page_url:

seleniumbase/fixtures/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class Values:
142142

143143

144144
class Warnings:
145+
SCREENSHOT_SKIPPED = "Skipping screenshot!"
145146
SCREENSHOT_UNDEFINED = "Unable to get screenshot!"
146147
PAGE_SOURCE_UNDEFINED = "Unable to get page source!"
147148
INVALID_RUN_COMMAND = "INVALID RUN COMMAND!"

seleniumbase/plugins/pytest_plugin.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def pytest_addoption(parser):
106106
--window-size=WIDTH,HEIGHT (Set the browser's starting window size.)
107107
--maximize (Start tests with the browser window maximized.)
108108
--screenshot (Save a screenshot at the end of each test.)
109+
--no-screenshot (No screenshots saved unless tests directly ask it.)
109110
--visual-baseline (Set the visual baseline for Visual/Layout tests.)
110111
--wire (Use selenium-wire's webdriver for replacing selenium webdriver.)
111112
--external-pdf (Set Chromium "plugins.always_open_pdf_externally":True.)
@@ -1083,8 +1084,20 @@ def pytest_addoption(parser):
10831084
action="store_true",
10841085
dest="save_screenshot",
10851086
default=False,
1086-
help="""Save a screenshot at the end of the test.
1087-
(Added to the "latest_logs/" folder.)""",
1087+
help="""Save a screenshot at the end of every test.
1088+
By default, this is only done for failures.
1089+
Will be saved in the "latest_logs/" folder.""",
1090+
)
1091+
parser.addoption(
1092+
"--no-screenshot",
1093+
"--no_screenshot",
1094+
"--ns",
1095+
action="store_true",
1096+
dest="no_screenshot",
1097+
default=False,
1098+
help="""No screenshots saved unless tests directly ask it.
1099+
This changes default behavior where screenshots are
1100+
saved for test failures and pytest-html reports.""",
10881101
)
10891102
parser.addoption(
10901103
"--visual_baseline",
@@ -1435,6 +1448,7 @@ def pytest_configure(config):
14351448
sb_config.window_size = config.getoption("window_size")
14361449
sb_config.maximize_option = config.getoption("maximize_option")
14371450
sb_config.save_screenshot = config.getoption("save_screenshot")
1451+
sb_config.no_screenshot = config.getoption("no_screenshot")
14381452
sb_config.visual_baseline = config.getoption("visual_baseline")
14391453
sb_config.use_wire = config.getoption("use_wire")
14401454
sb_config.external_pdf = config.getoption("external_pdf")
@@ -1559,12 +1573,14 @@ def pytest_configure(config):
15591573
if sb_config.dash_title:
15601574
constants.Dashboard.TITLE = sb_config.dash_title.replace("_", " ")
15611575

1576+
if sb_config.save_screenshot and sb_config.no_screenshot:
1577+
sb_config.save_screenshot = False # "no_screenshot" has priority
1578+
15621579
if (
15631580
sb_config._multithreaded
15641581
and "--co" not in sys_argv
15651582
and "--collect-only" not in sys_argv
15661583
):
1567-
from seleniumbase.core import log_helper
15681584
from seleniumbase.core import download_helper
15691585
from seleniumbase.core import proxy_helper
15701586

seleniumbase/plugins/selenium_plugin.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,20 @@ def options(self, parser, env):
770770
action="store_true",
771771
dest="save_screenshot",
772772
default=False,
773-
help="""Save a screenshot at the end of the test.
774-
(Added to the "latest_logs/" folder.)""",
773+
help="""Save a screenshot at the end of every test.
774+
By default, this is only done for failures.
775+
Will be saved in the "latest_logs/" folder.""",
776+
)
777+
parser.add_option(
778+
"--no-screenshot",
779+
"--no_screenshot",
780+
"--ns",
781+
action="store_true",
782+
dest="no_screenshot",
783+
default=False,
784+
help="""No screenshots saved unless tests directly ask it.
785+
This changes default behavior where screenshots are
786+
saved for test failures and pytest-html reports.""",
775787
)
776788
parser.add_option(
777789
"--visual_baseline",
@@ -948,14 +960,18 @@ def beforeTest(self, test):
948960
test.test._disable_beforeunload = self.options._disable_beforeunload
949961
test.test.window_size = self.options.window_size
950962
test.test.maximize_option = self.options.maximize_option
963+
if self.options.save_screenshot and self.options.no_screenshot:
964+
self.options.save_screenshot = False # no_screenshot has priority
951965
test.test.save_screenshot_after_test = self.options.save_screenshot
966+
test.test.no_screenshot_after_test = self.options.no_screenshot
952967
test.test.visual_baseline = self.options.visual_baseline
953968
test.test.use_wire = self.options.use_wire
954969
test.test.external_pdf = self.options.external_pdf
955970
test.test.timeout_multiplier = self.options.timeout_multiplier
956971
test.test.dashboard = False
957972
test.test._multithreaded = False
958973
test.test._reuse_session = False
974+
sb_config.no_screenshot = test.test.no_screenshot_after_test
959975
if test.test.servername != "localhost":
960976
# Using Selenium Grid
961977
# (Set --server="127.0.0.1" for localhost Grid)

0 commit comments

Comments
 (0)