2727DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png"
2828DEFAULT_FILENAME_ELEMENT = "selenium-element-screenshot-{index}.png"
2929EMBED = "EMBED"
30+ BASE64 = "BASE64"
31+ EMBEDDED_OPTIONS = [EMBED , BASE64 ]
3032DEFAULT_FILENAME_PDF = "selenium-page-{index}.pdf"
3133
3234
@@ -59,6 +61,8 @@ def set_screenshot_directory(self, path: Union[None, str]) -> str:
5961 path = None
6062 elif path .upper () == EMBED :
6163 path = EMBED
64+ elif path .upper () == BASE64 :
65+ path = BASE64
6266 else :
6367 path = os .path .abspath (path )
6468 self ._create_directory (path )
@@ -79,7 +83,14 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
7983
8084 If ``filename`` equals to EMBED (case insensitive), then screenshot
8185 is embedded as Base64 image to the log.html. In this case file is not
82- created in the filesystem.
86+ created in the filesystem. If ``filename`` equals to BASE64 (case
87+ insensitive), then the base64 string is returned and the screenshot
88+ is embedded to the log. This allows one to reuse the image elsewhere
89+ in the report.
90+
91+ Example:
92+ | ${ss}= | `Capture Page Screenshot` | BASE64 |
93+ | Set Test Message | *HTML*Test Success<p><img src="data:image/png;base64,${ss}" width="256px"> |
8394
8495 Starting from SeleniumLibrary 1.8, if ``filename`` contains marker
8596 ``{index}``, it will be automatically replaced with an unique running
@@ -89,9 +100,10 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
89100 format string syntax].
90101
91102 An absolute path to the created screenshot file is returned or if
92- ``filename`` equals to EMBED, word `EMBED` is returned.
103+ ``filename`` equals to EMBED, word `EMBED` is returned. If ``filename``
104+ equals to BASE64, the base64 string containing the screenshot is returned.
93105
94- Support for EMBED is new in SeleniumLibrary 4.2
106+ Support for BASE64 is new in SeleniumLibrary 6.8
95107
96108 Examples:
97109 | `Capture Page Screenshot` | |
@@ -111,8 +123,9 @@ def capture_page_screenshot(self, filename: str = DEFAULT_FILENAME_PAGE) -> str:
111123 if not self .drivers .current :
112124 self .info ("Cannot capture screenshot because no browser is open." )
113125 return
114- if self ._decide_embedded (filename ):
115- return self ._capture_page_screen_to_log ()
126+ is_embedded , method = self ._decide_embedded (filename )
127+ if is_embedded :
128+ return self ._capture_page_screen_to_log (method )
116129 return self ._capture_page_screenshot_to_file (filename )
117130
118131 def _capture_page_screenshot_to_file (self , filename ):
@@ -123,9 +136,11 @@ def _capture_page_screenshot_to_file(self, filename):
123136 self ._embed_to_log_as_file (path , 800 )
124137 return path
125138
126- def _capture_page_screen_to_log (self ):
139+ def _capture_page_screen_to_log (self , return_val ):
127140 screenshot_as_base64 = self .driver .get_screenshot_as_base64 ()
128- self ._embed_to_log_as_base64 (screenshot_as_base64 , 800 )
141+ base64_str = self ._embed_to_log_as_base64 (screenshot_as_base64 , 800 )
142+ if return_val == BASE64 :
143+ return base64_str
129144 return EMBED
130145
131146 @keyword
@@ -140,27 +155,34 @@ def capture_element_screenshot(
140155 See the `Locating elements` section for details about the locator
141156 syntax.
142157
143- An absolute path to the created element screenshot is returned.
158+ An absolute path to the created element screenshot is returned. If the ``filename``
159+ equals to BASE64 (case insensitive), then the base64 string is returned in addition
160+ to the screenshot embedded to the log. See ``Capture Page Screenshot`` for more
161+ information.
144162
145163 Support for capturing the screenshot from an element has limited support
146164 among browser vendors. Please check the browser vendor driver documentation
147165 does the browser support capturing a screenshot from an element.
148166
149167 New in SeleniumLibrary 3.3. Support for EMBED is new in SeleniumLibrary 4.2.
168+ Support for BASE64 is new in SeleniumLibrary 6.8.
150169
151170 Examples:
152171 | `Capture Element Screenshot` | id:image_id | |
153172 | `Capture Element Screenshot` | id:image_id | ${OUTPUTDIR}/id_image_id-1.png |
154173 | `Capture Element Screenshot` | id:image_id | EMBED |
174+ | ${ess}= | `Capture Element Screenshot` | id:image_id | BASE64 |
175+
155176 """
156177 if not self .drivers .current :
157178 self .info (
158179 "Cannot capture screenshot from element because no browser is open."
159180 )
160181 return
161182 element = self .find_element (locator , required = True )
162- if self ._decide_embedded (filename ):
163- return self ._capture_element_screen_to_log (element )
183+ is_embedded , method = self ._decide_embedded (filename )
184+ if is_embedded :
185+ return self ._capture_element_screen_to_log (element , method )
164186 return self ._capture_element_screenshot_to_file (element , filename )
165187
166188 def _capture_element_screenshot_to_file (self , element , filename ):
@@ -171,8 +193,10 @@ def _capture_element_screenshot_to_file(self, element, filename):
171193 self ._embed_to_log_as_file (path , 400 )
172194 return path
173195
174- def _capture_element_screen_to_log (self , element ):
175- self ._embed_to_log_as_base64 (element .screenshot_as_base64 , 400 )
196+ def _capture_element_screen_to_log (self , element , return_val ):
197+ base64_str = self ._embed_to_log_as_base64 (element .screenshot_as_base64 , 400 )
198+ if return_val == BASE64 :
199+ return base64_str
176200 return EMBED
177201
178202 @property
@@ -184,20 +208,20 @@ def _screenshot_root_directory(self, value):
184208 self .ctx .screenshot_root_directory = value
185209
186210 def _decide_embedded (self , filename ):
187- filename = filename .lower ()
211+ filename = filename .upper ()
188212 if (
189- filename == DEFAULT_FILENAME_PAGE
190- and self ._screenshot_root_directory == EMBED
213+ filename == DEFAULT_FILENAME_PAGE . upper ()
214+ and self ._screenshot_root_directory in EMBEDDED_OPTIONS
191215 ):
192- return True
216+ return True , self . _screenshot_root_directory
193217 if (
194- filename == DEFAULT_FILENAME_ELEMENT
195- and self ._screenshot_root_directory == EMBED
218+ filename == DEFAULT_FILENAME_ELEMENT . upper ()
219+ and self ._screenshot_root_directory in EMBEDDED_OPTIONS
196220 ):
197- return True
198- if filename == EMBED . lower () :
199- return True
200- return False
221+ return True , self . _screenshot_root_directory
222+ if filename in EMBEDDED_OPTIONS :
223+ return True , self . _screenshot_root_directory
224+ return False , None
201225
202226 def _get_screenshot_path (self , filename ):
203227 if self ._screenshot_root_directory != EMBED :
0 commit comments