Skip to content

Commit 4be1e45

Browse files
committed
Add methods for folder creation and saving elements as images
1 parent feea122 commit 4be1e45

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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.

0 commit comments

Comments
 (0)