Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handling for failed screenshot take #890

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading