Skip to content

Commit

Permalink
Merge pull request #890 from dhlavac/screenshot_fail_handled
Browse files Browse the repository at this point in the history
Added handling for failed screenshot take
  • Loading branch information
dhlavac authored Nov 22, 2024
2 parents 48dbe00 + cf70343 commit fbc5c32
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions testsuite/tests/ui/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,46 +466,51 @@ def get_resultsdir_path(node):
return path


# pylint: disable=too-many-locals
def fullpage_screenshot(driver, file_path):
"""
A full-page screenshot function. It scroll the website and screenshots it.
A full-page screenshot function. It scrolls the website and screenshots it.
- Creates multiple files
- Screenshots are made only vertically (on Y axis)
"""
# Removal of the height: 100% style, that disables scroll.
driver.execute_script("document.body.style.height = 'unset'")
driver.execute_script("document.body.parentNode.style.height = 'unset'")
try:
# Removal of the height: 100% style, that disables scroll.
driver.execute_script("document.body.style.height = 'unset'")
driver.execute_script("document.body.parentNode.style.height = 'unset'")

total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_height = driver.execute_script("return window.innerHeight")
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_height = driver.execute_script("return window.innerHeight")

screenshot_bytes = driver.get_screenshot_as_png()
screenshot = Image.open(io.BytesIO(screenshot_bytes))
width, height = screenshot.size
del screenshot

scaling_constant = float(height) / float(viewport_height)
stitched_image = Image.new("RGB", (width, int(total_height * scaling_constant)))
part = 0

for scroll in range(0, total_height, viewport_height):
driver.execute_script("window.scrollTo({0}, {1})".format(0, scroll))
screenshot_bytes = driver.get_screenshot_as_png()
screenshot = Image.open(io.BytesIO(screenshot_bytes))

if scroll + viewport_height > total_height:
offset = (0, int(math.ceil((total_height - viewport_height) * scaling_constant)))
else:
offset = (0, int(math.ceil(scroll * scaling_constant)))

stitched_image.paste(screenshot, offset)
width, height = screenshot.size
del screenshot
part += 1

date = datetime.today().strftime("%Y-%m-%d-%H:%M:%S")
fullpath = f"{file_path}/{date}.png"
stitched_image.save(fullpath)
return fullpath
scaling_constant = float(height) / float(viewport_height)
stitched_image = Image.new("RGB", (width, int(total_height * scaling_constant)))
part = 0

for scroll in range(0, total_height, viewport_height):
driver.execute_script("window.scrollTo({0}, {1})".format(0, scroll))
screenshot_bytes = driver.get_screenshot_as_png()
screenshot = Image.open(io.BytesIO(screenshot_bytes))

if scroll + viewport_height > total_height:
offset = (0, int(math.ceil((total_height - viewport_height) * scaling_constant)))
else:
offset = (0, int(math.ceil(scroll * scaling_constant)))

stitched_image.paste(screenshot, offset)
del screenshot
part += 1

date = datetime.today().strftime("%Y-%m-%d-%H:%M:%S")
fullpath = f"{file_path}/{date}.png"
stitched_image.save(fullpath)
return fullpath
# pylint: disable=broad-exception-caught
except Exception as e:
return f"Error: Failed to take full-page screenshot. Details: {str(e)}"


@pytest.fixture(scope="module")
Expand Down

0 comments on commit fbc5c32

Please sign in to comment.