Skip to content

Commit 27e8147

Browse files
author
Sergio Freire
committed
first set of proposed changed with a import config setting
1 parent 053e4b9 commit 27e8147

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

atest/acceptance/resource.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
*** Setting ***
2-
Library SeleniumLibrary run_on_failure=Nothing implicit_wait=0.2 seconds plugins=RunningKeyword.RunningKeyword
2+
Library SeleniumLibrary run_on_failure=Nothing implicit_wait=0.2 seconds plugins=RunningKeyword.RunningKeyword embed_screenshots=True
33
Library Collections
44
Library OperatingSystem
55
Library DateTime

src/SeleniumLibrary/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ class SeleniumLibrary(DynamicCore):
405405

406406
def __init__(self, timeout=5.0, implicit_wait=0.0,
407407
run_on_failure='Capture Page Screenshot',
408-
screenshot_root_directory=None, plugins=None,
408+
screenshot_root_directory=None, embed_screenshots=False, plugins=None,
409409
event_firing_webdriver=None):
410410
"""SeleniumLibrary can be imported with several optional arguments.
411411
@@ -431,6 +431,7 @@ def __init__(self, timeout=5.0, implicit_wait=0.0,
431431
= RunOnFailureKeywords.resolve_keyword(run_on_failure)
432432
self._running_on_failure_keyword = False
433433
self.screenshot_root_directory = screenshot_root_directory
434+
self.embed_screenshots = embed_screenshots
434435
self._element_finder = ElementFinder(self)
435436
self._plugin_keywords = []
436437
libraries = [

src/SeleniumLibrary/keywords/screenshot.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,38 @@
1515
# limitations under the License.
1616

1717
import os
18+
from os.path import basename
1819

1920
from robot.utils import get_link_path
2021

2122
from SeleniumLibrary.base import LibraryComponent, keyword
2223
from SeleniumLibrary.utils import is_noney
2324
from SeleniumLibrary.utils.path_formatter import _format_path
24-
25+
import base64
2526

2627
class ScreenshotKeywords(LibraryComponent):
2728

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+
2850
@keyword
2951
def set_screenshot_directory(self, path):
3052
"""Sets the directory for captured screenshots.
@@ -148,3 +170,8 @@ def _embed_to_log(self, path, width):
148170
self.info('</td></tr><tr><td colspan="3">'
149171
'<a href="{src}"><img src="{src}" width="{width}px"></a>'
150172
.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

Comments
 (0)