Skip to content

Commit 963c07c

Browse files
authored
Merge pull request #312 from seleniumbase/localized-screenshots
Add ability to save page elements as screenshot files
2 parents feea122 + c20a557 commit 963c07c

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ report.xml
7070
# Tours
7171
tours_exported
7272

73+
# Images
74+
images_exported
75+
7376
# Automated Visual Testing
7477
visual_baseline
7578

examples/image_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Pull an image from a website and save it as a PNG file.
3+
"""
4+
5+
from seleniumbase import BaseCase
6+
7+
8+
class ImageTest(BaseCase):
9+
10+
def test_pull_image_from_website(self):
11+
self.open("https://xkcd.com/1117/")
12+
selector = "#comic"
13+
file_name = "comic.png"
14+
folder = "images_exported"
15+
self.save_element_as_image_file(selector, file_name, folder)
16+
print('"%s/%s" has been saved!' % (folder, file_name))

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ self.print_unique_links_with_status_codes()
182182

183183
self.safe_execute_script(script)
184184

185+
self.create_folder(folder)
186+
187+
self.save_element_as_image_file(selector, file_name, folder=None)
188+
185189
self.download_file(file_url, destination_folder=None)
186190

187191
self.save_file_as(file_url, new_file_name, destination_folder=None)

seleniumbase/fixtures/base_case.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,39 @@ def safe_execute_script(self, script):
16731673
self.activate_jquery() # It's a good thing we can define it here
16741674
self.execute_script(script)
16751675

1676+
def create_folder(self, folder):
1677+
""" Creates a folder of the given name if it doesn't already exist. """
1678+
if folder.endswith("/"):
1679+
folder = folder[:-1]
1680+
if len(folder) < 1:
1681+
raise Exception("Minimum folder name length = 1.")
1682+
if not os.path.exists(folder):
1683+
try:
1684+
os.makedirs(folder)
1685+
except Exception:
1686+
pass
1687+
1688+
def save_element_as_image_file(self, selector, file_name, folder=None):
1689+
""" Take a screenshot of an element and save it as an image file.
1690+
If no folder is specified, will save it to the current folder. """
1691+
element = self.find_element(selector)
1692+
element_png = element.screenshot_as_png
1693+
if len(file_name.split('.')[0]) < 1:
1694+
raise Exception("Error: file_name length must be > 0.")
1695+
if not file_name.endswith(".png"):
1696+
file_name = file_name + ".png"
1697+
image_file_path = None
1698+
if folder:
1699+
if folder.endswith("/"):
1700+
folder = folder[:-1]
1701+
if len(folder) > 0:
1702+
self.create_folder(folder)
1703+
image_file_path = "%s/%s" % (folder, file_name)
1704+
if not image_file_path:
1705+
image_file_path = file_name
1706+
with open(image_file_path, "wb") as file:
1707+
file.write(element_png)
1708+
16761709
def download_file(self, file_url, destination_folder=None):
16771710
""" Downloads the file from the url to the destination folder.
16781711
If no destination folder is specified, the default one is used.

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.22.8',
20+
version='1.22.9',
2121
description='Reliable Browser Automation & Testing Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)