Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to embed screenshots in log.html as base64 content #332

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions AppiumLibrary/keywords/_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,42 @@
import robot
from .keywordgroup import KeywordGroup


class _ScreenshotKeywords(KeywordGroup):

def __init__(self):
self._screenshot_index = 0

# Public

def capture_page_screenshot(self, filename=None):
"""Takes a screenshot of the current page and embeds it into the log.

`filename` argument specifies the name of the file to write the
screenshot into. If no `filename` is given, the screenshot is saved into file
`appium-screenshot-<counter>.png` under the directory where
the Robot Framework log file is written into. The `filename` is
also considered relative to the same directory, if it is not
given in absolute format.

`css` can be used to modify how the screenshot is taken. By default
the background color is changed to avoid possible problems with
background leaking when the page layout is somehow broken.
"""
path, link = self._get_screenshot_paths(filename)
screenshot into. If no `filename` is given, the screenshot will be
embedded as Base64 image to the log.html. In this case no file is created in the filesystem.

if hasattr(self._current_application(), 'get_screenshot_as_file'):
self._current_application().get_screenshot_as_file(path)
Warning: this behavior is new in 1.7. Previously if no filename was given
the screenshots where stored as separate files named `appium-screenshot-<counter>.png`
"""
if filename:
path, link = self._get_screenshot_paths(filename)

if hasattr(self._current_application(), 'get_screenshot_as_file'):
self._current_application().get_screenshot_as_file(path)
else:
self._current_application().save_screenshot(path)

# Image is shown on its own row and thus prev row is closed on purpose
self._html('</td></tr><tr><td colspan="3"><a href="%s">'
'<img src="%s" width="800px"></a>' % (link, link))
return path
else:
self._current_application().save_screenshot(path)

# Image is shown on its own row and thus prev row is closed on purpose
self._html('</td></tr><tr><td colspan="3"><a href="%s">'
'<img src="%s" width="800px"></a>' % (link, link))
return path
base64_screenshot = self._current_application().get_screenshot_as_base64()
self._html('</td></tr><tr><td colspan="3">'
'<img src="data:image/png;base64, %s" width="800px">' % base64_screenshot)
return None

# Private

def _get_screenshot_paths(self, filename):
if not filename:
self._screenshot_index += 1
filename = 'appium-screenshot-%d.png' % self._screenshot_index
else:
filename = filename.replace('/', os.sep)
filename = filename.replace('/', os.sep)
logdir = self._get_log_dir()
path = os.path.join(logdir, filename)
link = robot.utils.get_link_path(path, logdir)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'robotframework >= 2.6.0',
'docutils >= 0.8.1',
'Appium-Python-Client >= 1.1.0',
'selenium >= 2.47.1, < 4',
'selenium >= 2.47.1',
'kitchen >= 1.2.4',
'six >= 1.10.0'
],
Expand Down