Skip to content

Commit b0833e3

Browse files
committed
feat: Add Capture Full Page Screenshot keyword
Fix #1459
1 parent 9368e3f commit b0833e3

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/SeleniumLibrary/keywords/screenshot.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ 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_full_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
68+
"""Takes a full page screenshot of the current page and embeds it into a log file.
69+
70+
See the `Capture Page Screenshot` section for details.
71+
"""
72+
return self.capture_page_screenshot(filename, full_screen = True)
73+
74+
@keyword
75+
def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE, full_screen: bool = False) -> str:
6876
"""Takes a screenshot of the current page and embeds it into a log file.
6977
7078
``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:
7886
is embedded as Base64 image to the log.html. In this case file is not
7987
created in the filesystem.
8088
89+
If ``full_screen`` is True, then a full page screenshot will be taken.
90+
8191
Starting from SeleniumLibrary 1.8, if ``filename`` contains marker
8292
``{index}``, it will be automatically replaced with an unique running
8393
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:
90100
91101
Support for EMBED is new in SeleniumLibrary 4.2
92102
103+
Support for full_screen is new in SeleniumLibrary 5.x.x
104+
93105
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 |
106+
| `Capture Page Screenshot` | | |
107+
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-1.png | |
108+
| ${path} = | `Capture Page Screenshot` | |
109+
| `File Should Exist` | ${OUTPUTDIR}/selenium-screenshot-2.png | |
110+
| `File Should Exist` | ${path} | |
111+
| `Capture Page Screenshot` | custom_name.png | |
112+
| `File Should Exist` | ${OUTPUTDIR}/custom_name.png | |
113+
| `Capture Page Screenshot` | custom_with_index_{index}.png | |
114+
| `File Should Exist` | ${OUTPUTDIR}/custom_with_index_1.png | |
115+
| `Capture Page Screenshot` | formatted_index_{index:03}.png | |
116+
| `File Should Exist` | ${OUTPUTDIR}/formatted_index_001.png | |
117+
| `Capture Page Screenshot` | EMBED | |
118+
| `File Should Not Exist` | EMBED | |
119+
| `Capture Full Page Screenshot` | EMBED | full_screen = True |
120+
| `File Should Not Exist` | EMBED | |
107121
"""
108122
if not self.drivers.current:
109123
self.info("Cannot capture screenshot because no browser is open.")
110124
return
111125
if self._decide_embedded(filename):
112-
return self._capture_page_screen_to_log()
113-
return self._capture_page_screenshot_to_file(filename)
126+
return self._capture_page_screen_to_log(full_screen)
127+
return self._capture_page_screenshot_to_file(filename, full_screen)
114128

115-
def _capture_page_screenshot_to_file(self, filename):
129+
def _capture_page_screenshot_to_file(self, filename, full_screen = False):
116130
path = self._get_screenshot_path(filename)
117131
self._create_directory(path)
118-
if not self.driver.save_screenshot(path):
119-
raise RuntimeError(f"Failed to save screenshot '{path}'.")
132+
if full_screen:
133+
if not self.driver.save_full_page_screenshot(path):
134+
raise RuntimeError(f"Failed to save full page screenshot '{path}'.")
135+
else:
136+
if not self.driver.save_screenshot(path):
137+
raise RuntimeError(f"Failed to save screenshot '{path}'.")
120138
self._embed_to_log_as_file(path, 800)
121139
return path
122140

123-
def _capture_page_screen_to_log(self):
124-
screenshot_as_base64 = self.driver.get_screenshot_as_base64()
141+
def _capture_page_screen_to_log(self, full_screen = False):
142+
if full_screen:
143+
screenshot_as_base64 = self.driver.get_full_page_screenshot_as_base64()
144+
else:
145+
screenshot_as_base64 = self.driver.get_screenshot_as_base64()
125146
self._embed_to_log_as_base64(screenshot_as_base64, 800)
126147
return EMBED
127148

utest/test/api/test_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setUpClass(cls):
2222
def test_no_libraries(self):
2323
for item in [None, "None", ""]:
2424
sl = SeleniumLibrary(plugins=item)
25-
self.assertEqual(len(sl.get_keyword_names()), 173)
25+
self.assertEqual(len(sl.get_keyword_names()), 174)
2626

2727
def test_parse_library(self):
2828
plugin = "path.to.MyLibrary"

0 commit comments

Comments
 (0)