Skip to content

Commit 7387374

Browse files
committed
Change the algorithm for taking screenshots
1 parent b91ae82 commit 7387374

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

seleniumbase/core/log_helper.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ def log_screenshot(test_logpath, driver):
1111
screenshot_name = settings.SCREENSHOT_NAME
1212
screenshot_path = "%s/%s" % (test_logpath, screenshot_name)
1313
try:
14-
driver.get_screenshot_as_file(screenshot_path)
14+
element = driver.find_element_by_tag_name('body')
15+
element_png = element.screenshot_as_png
16+
with open(screenshot_path, "wb") as file:
17+
file.write(element_png)
1518
except Exception:
16-
print("WARNING: Unable to get screenshot for failure logs!")
19+
try:
20+
driver.get_screenshot_as_file(screenshot_path)
21+
except Exception:
22+
print("WARNING: Unable to get screenshot for failure logs!")
1723

1824

1925
def log_test_failure_data(test, test_logpath, driver, browser):

seleniumbase/fixtures/base_case.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,7 @@ def switch_to_default_window(self):
24492449
self.switch_to_window(0)
24502450

24512451
def save_screenshot(self, name, folder=None):
2452+
""" The screenshot will be in PNG format. """
24522453
return page_actions.save_screenshot(self.driver, name, folder)
24532454

24542455
def get_new_driver(self, browser=None, headless=None,

seleniumbase/fixtures/page_actions.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,25 @@ def save_screenshot(driver, name, folder=None):
369369
"""
370370
Saves a screenshot to the current directory (or to a subfolder if provided)
371371
If the folder provided doesn't exist, it will get created.
372+
The screenshot will be in PNG format.
372373
"""
374+
if "." not in name:
375+
name = name + ".png"
373376
if folder:
374377
abs_path = os.path.abspath('.')
375378
file_path = abs_path + "/%s" % folder
376379
if not os.path.exists(file_path):
377380
os.makedirs(file_path)
378-
screenshot_file = "%s/%s" % (file_path, name)
381+
screenshot_path = "%s/%s" % (file_path, name)
379382
else:
380-
screenshot_file = name
381-
driver.get_screenshot_as_file(screenshot_file)
383+
screenshot_path = name
384+
try:
385+
element = driver.find_element_by_tag_name('body')
386+
element_png = element.screenshot_as_png
387+
with open(screenshot_path, "wb") as file:
388+
file.write(element_png)
389+
except Exception:
390+
driver.get_screenshot_as_file(screenshot_path)
382391

383392

384393
def _get_last_page(driver):

0 commit comments

Comments
 (0)