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

Fix #1459: Add Capture Full Page Screenshot #1762

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Expand Up @@ -159,7 +159,7 @@ Tests
When submitting a pull request with a new feature or a fix, you should
always include tests for your changes. These tests prove that your
changes work, help prevent bugs in the future, and help document what
your changes do. Depending an the change, you may need
your changes do. Depending on the change, you may need
``acceptance tests``\ *, ``unit tests``* or both.

Make sure to run all of the tests before submitting a pull request to be
Expand Down
57 changes: 36 additions & 21 deletions src/SeleniumLibrary/keywords/screenshot.py
Expand Up @@ -64,7 +64,7 @@ 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_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
Expand All @@ -78,6 +78,10 @@ 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.
Only firefox driver support this. On other drivers, this option has
no effect.

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,
Expand All @@ -90,38 +94,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.2.0

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 and hasattr(self.driver, 'save_full_page_screenshot'):
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 and hasattr(self.driver, 'get_full_page_screenshot_as_base64'):
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

Expand Down