Skip to content

Commit a3e3186

Browse files
committed
feat: Allow full_screen screenshots on Firefox
Fix robotframework#1459
1 parent d091adb commit a3e3186

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/SeleniumLibrary/keywords/screenshot.py

+36-21
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def set_screenshot_directory(self, path: Union[None, str]) -> str:
6464
return previous
6565

6666
@keyword
67-
def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
67+
def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE, full_screen: bool = False) -> str:
6868
"""Takes a screenshot of the current page and embeds it into a log file.
6969
7070
``filename`` argument specifies the name of the file to write the
@@ -78,6 +78,10 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
7878
is embedded as Base64 image to the log.html. In this case file is not
7979
created in the filesystem.
8080
81+
If ``full_screen`` is True, then a full page screenshot will be taken.
82+
Only firefox driver support this. On other drivers, this option has
83+
no effect.
84+
8185
Starting from SeleniumLibrary 1.8, if ``filename`` contains marker
8286
``{index}``, it will be automatically replaced with an unique running
8387
index, preventing files to be overwritten. Indices start from 1,
@@ -90,38 +94,49 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
9094
9195
Support for EMBED is new in SeleniumLibrary 4.2
9296
97+
Support for full_screen is new in SeleniumLibrary 5.2.0
98+
9399
Examples:
94-
| `Capture Page Screenshot` | |
95-
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-1.png |
96-
| ${path} = | `Capture Page Screenshot` |
97-
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-2.png |
98-
| `File Should Exist` | ${path} |
99-
| `Capture Page Screenshot` | custom_name.png |
100-
| `File Should Exist` | ${OUTPUTDIR}/custom_name.png |
101-
| `Capture Page Screenshot` | custom_with_index_{index}.png |
102-
| `File Should Exist` | ${OUTPUTDIR}/custom_with_index_1.png |
103-
| `Capture Page Screenshot` | formatted_index_{index:03}.png |
104-
| `File Should Exist` | ${OUTPUTDIR}/formatted_index_001.png |
105-
| `Capture Page Screenshot` | EMBED |
106-
| `File Should Not Exist` | EMBED |
100+
| `Capture Page Screenshot` | | |
101+
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-1.png | |
102+
| ${path} = | `Capture Page Screenshot` | |
103+
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-2.png | |
104+
| `File Should Exist` | ${path} | |
105+
| `Capture Page Screenshot` | custom_name.png | |
106+
| `File Should Exist` | ${OUTPUTDIR}/custom_name.png | |
107+
| `Capture Page Screenshot` | custom_with_index_{index}.png | |
108+
| `File Should Exist` | ${OUTPUTDIR}/custom_with_index_1.png | |
109+
| `Capture Page Screenshot` | formatted_index_{index:03}.png | |
110+
| `File Should Exist` | ${OUTPUTDIR}/formatted_index_001.png | |
111+
| `Capture Page Screenshot` | EMBED | |
112+
| `File Should Not Exist` | EMBED | |
113+
| `Capture Full Page Screenshot` | EMBED | full_screen = True |
114+
| `File Should Not Exist` | EMBED | |
107115
"""
108116
if not self.drivers.current:
109117
self.info("Cannot capture screenshot because no browser is open.")
110118
return
111119
if self._decide_embedded(filename):
112-
return self._capture_page_screen_to_log()
113-
return self._capture_page_screenshot_to_file(filename)
120+
return self._capture_page_screen_to_log(full_screen)
121+
return self._capture_page_screenshot_to_file(filename, full_screen)
114122

115-
def _capture_page_screenshot_to_file(self, filename):
123+
def _capture_page_screenshot_to_file(self, filename, full_screen = False):
116124
path = self._get_screenshot_path(filename)
117125
self._create_directory(path)
118-
if not self.driver.save_screenshot(path):
119-
raise RuntimeError(f"Failed to save screenshot '{path}'.")
126+
if full_screen and hasattr(self.driver, 'save_full_page_screenshot'):
127+
if not self.driver.save_full_page_screenshot(path):
128+
raise RuntimeError(f"Failed to save full page screenshot '{path}'.")
129+
else:
130+
if not self.driver.save_screenshot(path):
131+
raise RuntimeError(f"Failed to save screenshot '{path}'.")
120132
self._embed_to_log_as_file(path, 800)
121133
return path
122134

123-
def _capture_page_screen_to_log(self):
124-
screenshot_as_base64 = self.driver.get_screenshot_as_base64()
135+
def _capture_page_screen_to_log(self, full_screen = False):
136+
if full_screen and hasattr(self.driver, 'get_full_page_screenshot_as_base64'):
137+
screenshot_as_base64 = self.driver.get_full_page_screenshot_as_base64()
138+
else:
139+
screenshot_as_base64 = self.driver.get_screenshot_as_base64()
125140
self._embed_to_log_as_base64(screenshot_as_base64, 800)
126141
return EMBED
127142

0 commit comments

Comments
 (0)