From b0833e31aea5393bb6f22e4243d6b200540410b4 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Sun, 12 Dec 2021 21:54:29 +0100 Subject: [PATCH] feat: Add Capture Full Page Screenshot keyword Fix #1459 --- src/SeleniumLibrary/keywords/screenshot.py | 63 ++++++++++++++-------- utest/test/api/test_plugins.py | 2 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 628361c8a..b7b8f6c71 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -64,7 +64,15 @@ def set_screenshot_directory(self, path: Union[None, str]) -> str: return previous @keyword - def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str: + def capture_full_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str: + """Takes a full page screenshot of the current page and embeds it into a log file. + + See the `Capture Page Screenshot` section for details. + """ + return self.capture_page_screenshot(filename, full_screen = True) + + @keyword + def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE, full_screen: bool = False) -> str: """Takes a screenshot of the current page and embeds it into a log file. ``filename`` argument specifies the name of the file to write the @@ -78,6 +86,8 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str: is embedded as Base64 image to the log.html. In this case file is not created in the filesystem. + If ``full_screen`` is True, then a full page screenshot will be taken. + Starting from SeleniumLibrary 1.8, if ``filename`` contains marker ``{index}``, it will be automatically replaced with an unique running index, preventing files to be overwritten. Indices start from 1, @@ -90,38 +100,49 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str: Support for EMBED is new in SeleniumLibrary 4.2 + Support for full_screen is new in SeleniumLibrary 5.x.x + Examples: - | `Capture Page Screenshot` | | - | `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-1.png | - | ${path} = | `Capture Page Screenshot` | - | `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-2.png | - | `File Should Exist` | ${path} | - | `Capture Page Screenshot` | custom_name.png | - | `File Should Exist` | ${OUTPUTDIR}/custom_name.png | - | `Capture Page Screenshot` | custom_with_index_{index}.png | - | `File Should Exist` | ${OUTPUTDIR}/custom_with_index_1.png | - | `Capture Page Screenshot` | formatted_index_{index:03}.png | - | `File Should Exist` | ${OUTPUTDIR}/formatted_index_001.png | - | `Capture Page Screenshot` | EMBED | - | `File Should Not Exist` | EMBED | + | `Capture Page Screenshot` | | | + | `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-1.png | | + | ${path} = | `Capture Page Screenshot` | | + | `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-2.png | | + | `File Should Exist` | ${path} | | + | `Capture Page Screenshot` | custom_name.png | | + | `File Should Exist` | ${OUTPUTDIR}/custom_name.png | | + | `Capture Page Screenshot` | custom_with_index_{index}.png | | + | `File Should Exist` | ${OUTPUTDIR}/custom_with_index_1.png | | + | `Capture Page Screenshot` | formatted_index_{index:03}.png | | + | `File Should Exist` | ${OUTPUTDIR}/formatted_index_001.png | | + | `Capture Page Screenshot` | EMBED | | + | `File Should Not Exist` | EMBED | | + | `Capture Full Page Screenshot` | EMBED | full_screen = True | + | `File Should Not Exist` | EMBED | | """ if not self.drivers.current: self.info("Cannot capture screenshot because no browser is open.") return if self._decide_embedded(filename): - return self._capture_page_screen_to_log() - return self._capture_page_screenshot_to_file(filename) + return self._capture_page_screen_to_log(full_screen) + return self._capture_page_screenshot_to_file(filename, full_screen) - def _capture_page_screenshot_to_file(self, filename): + def _capture_page_screenshot_to_file(self, filename, full_screen = False): path = self._get_screenshot_path(filename) self._create_directory(path) - if not self.driver.save_screenshot(path): - raise RuntimeError(f"Failed to save screenshot '{path}'.") + if full_screen: + if not self.driver.save_full_page_screenshot(path): + raise RuntimeError(f"Failed to save full page screenshot '{path}'.") + else: + if not self.driver.save_screenshot(path): + raise RuntimeError(f"Failed to save screenshot '{path}'.") self._embed_to_log_as_file(path, 800) return path - def _capture_page_screen_to_log(self): - screenshot_as_base64 = self.driver.get_screenshot_as_base64() + def _capture_page_screen_to_log(self, full_screen = False): + if full_screen: + screenshot_as_base64 = self.driver.get_full_page_screenshot_as_base64() + else: + screenshot_as_base64 = self.driver.get_screenshot_as_base64() self._embed_to_log_as_base64(screenshot_as_base64, 800) return EMBED diff --git a/utest/test/api/test_plugins.py b/utest/test/api/test_plugins.py index 7218e637b..fd4d06ff4 100644 --- a/utest/test/api/test_plugins.py +++ b/utest/test/api/test_plugins.py @@ -22,7 +22,7 @@ def setUpClass(cls): def test_no_libraries(self): for item in [None, "None", ""]: sl = SeleniumLibrary(plugins=item) - self.assertEqual(len(sl.get_keyword_names()), 173) + self.assertEqual(len(sl.get_keyword_names()), 174) def test_parse_library(self): plugin = "path.to.MyLibrary"