From 5d289f38a40a4c40cc7655793b832bb2ce8a47fc Mon Sep 17 00:00:00 2001 From: Dominik Hlavac Duran Date: Thu, 21 Nov 2024 16:50:20 +0100 Subject: [PATCH] Added handling for failed screenshot take --- testsuite/tests/ui/conftest.py | 61 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/testsuite/tests/ui/conftest.py b/testsuite/tests/ui/conftest.py index 92a46329..0aa73a50 100644 --- a/testsuite/tests/ui/conftest.py +++ b/testsuite/tests/ui/conftest.py @@ -468,44 +468,47 @@ def get_resultsdir_path(node): 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 + except Exception as e: + return f"Error: Failed to take full-page screenshot. Details: {str(e)}" @pytest.fixture(scope="module")