|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
17 | 17 | import os
|
| 18 | +from os.path import basename |
18 | 19 |
|
19 | 20 | from robot.utils import get_link_path
|
20 | 21 |
|
21 | 22 | from SeleniumLibrary.base import LibraryComponent, keyword
|
22 | 23 | from SeleniumLibrary.utils import is_noney
|
23 | 24 | from SeleniumLibrary.utils.path_formatter import _format_path
|
24 |
| - |
| 25 | +import base64 |
25 | 26 |
|
26 | 27 | class ScreenshotKeywords(LibraryComponent):
|
27 | 28 |
|
| 29 | + @keyword |
| 30 | + def set_embed_screenshots(self, embed_screenshots): |
| 31 | + """Sets the directory for captured screenshots. |
| 32 | +
|
| 33 | + ``path`` argument specifies the absolute path to a directory where |
| 34 | + the screenshots should be written to. If the directory does not |
| 35 | + exist, it will be created. The directory can also be set when |
| 36 | + `importing` the library. If it is not configured anywhere, |
| 37 | + screenshots are saved to the same directory where Robot Framework's |
| 38 | + log file is written. |
| 39 | +
|
| 40 | + The previous value is returned and can be used to restore |
| 41 | + the original value later if needed. |
| 42 | +
|
| 43 | + Returning the previous value is new in SeleniumLibrary 3.0. |
| 44 | + The persist argument was removed in SeleniumLibrary 3.2. |
| 45 | + """ |
| 46 | + previous = self.ctx.embed_screenshots |
| 47 | + self.ctx.embed_screenshots = path |
| 48 | + return previous |
| 49 | + |
28 | 50 | @keyword
|
29 | 51 | def set_screenshot_directory(self, path):
|
30 | 52 | """Sets the directory for captured screenshots.
|
@@ -148,3 +170,8 @@ def _embed_to_log(self, path, width):
|
148 | 170 | self.info('</td></tr><tr><td colspan="3">'
|
149 | 171 | '<a href="{src}"><img src="{src}" width="{width}px"></a>'
|
150 | 172 | .format(src=get_link_path(path, self.log_dir), width=width), html=True)
|
| 173 | + self.info('screenshots:{s}'.format(s=self.ctx.embed_screenshots)) |
| 174 | + if self.ctx.embed_screenshots: |
| 175 | + data = open(path, "r").read() |
| 176 | + self.info('<a href="{src}" download="{basename}"><img src="data:image/png;base64,{encodedfile}" width="{width}px"></a>' |
| 177 | + .format(src=get_link_path(path, self.log_dir), encodedfile=base64.b64encode(data), basename=basename(path), width=width), html=True) |
0 commit comments