-
Notifications
You must be signed in to change notification settings - Fork 786
Added screenshot_root_directory #443
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
Changes from all commits
6a90fd7
1f606f3
07effe9
e43e782
dbd00d2
1ba05a0
f5a32ff
189b532
ab1e481
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,37 @@ | ||
| import os, errno | ||
| import robot | ||
| import os, errno | ||
|
|
||
| from Selenium2Library import utils | ||
| from keywordgroup import KeywordGroup | ||
| from robot.libraries.BuiltIn import RobotNotRunningError | ||
|
|
||
|
|
||
| class _ScreenshotKeywords(KeywordGroup): | ||
|
|
||
| def __init__(self): | ||
| self._screenshot_index = 0 | ||
| self._screenshot_path_stack = [] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's likely that this could cause an issue. If the same scope sets two screenshot paths then only the first would be popped off when the scope exited. That could cause an issue where the wrong path is used for other scopes... EDIT: I've decided to go ahead and use what I've got. Being that this is a private part of the API I can solidify this in the next release. |
||
| self.screenshot_root_directory = None | ||
|
|
||
| # Public | ||
|
|
||
| def set_screenshot_directory(self, path, persist=False): | ||
| """Sets the root output directory for captured screenshots. | ||
|
|
||
| ``path`` argument specifies the location to where the screenshots should | ||
| be written to. If the specified ``path`` does not exist, it will be created. | ||
| Setting ``persist`` specifies that the given ``path`` should | ||
| be used for the rest of the test execution, otherwise the path will be restored | ||
| at the end of the currently executing scope. | ||
| """ | ||
| self._create_directory(path) | ||
| if persist is False: | ||
| self._screenshot_path_stack.append(self.screenshot_root_directory) | ||
| # Restore after current scope ends | ||
| utils.events.on('scope_end', 'current', self._restore_screenshot_directory) | ||
|
|
||
| self.screenshot_root_directory = path | ||
|
|
||
| def capture_page_screenshot(self, filename=None): | ||
| """Takes a screenshot of the current page and embeds it into the log. | ||
|
|
||
|
|
@@ -18,22 +41,14 @@ def capture_page_screenshot(self, filename=None): | |
| 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. If an absolute or relative path is given | ||
| but the path does not exist it will be created. | ||
| but the path does not exist it will be created. | ||
|
|
||
| `css` can be used to modify how the screenshot is taken. By default | ||
| the bakground color is changed to avoid possible problems with | ||
| background leaking when the page layout is somehow broken. | ||
| """ | ||
| path, link = self._get_screenshot_paths(filename) | ||
|
|
||
| target_dir = os.path.dirname(path) | ||
| if not os.path.exists(target_dir): | ||
| try: | ||
| os.makedirs(target_dir) | ||
| except OSError as exc: | ||
| if exc.errno == errno.EEXIST and os.path.isdir(target_dir): | ||
| pass | ||
| else: raise | ||
| self._create_directory(path) | ||
|
|
||
| if hasattr(self._current_browser(), 'get_screenshot_as_file'): | ||
| if not self._current_browser().get_screenshot_as_file(path): | ||
|
|
@@ -47,14 +62,42 @@ def capture_page_screenshot(self, filename=None): | |
| '<img src="%s" width="800px"></a>' % (link, link)) | ||
|
|
||
| # Private | ||
| def _create_directory(self, path): | ||
| target_dir = os.path.dirname(path) | ||
| if not os.path.exists(target_dir): | ||
| try: | ||
| os.makedirs(target_dir) | ||
| except OSError as exc: | ||
| if exc.errno == errno.EEXIST and os.path.isdir(target_dir): | ||
| pass | ||
| else: | ||
| raise | ||
|
|
||
| def _get_screenshot_directory(self): | ||
|
|
||
| # Use screenshot root directory if set | ||
| if self.screenshot_root_directory is not None: | ||
| return self.screenshot_root_directory | ||
|
|
||
| # Otherwise use RF's log directory | ||
| try: | ||
| return self._get_log_dir() | ||
|
|
||
| # Unless robotframework isn't running, then use working directory | ||
| except RobotNotRunningError: | ||
| return os.getcwd() | ||
|
|
||
| # should only be called by set_screenshot_directory | ||
| def _restore_screenshot_directory(self): | ||
| self.screenshot_root_directory = self._screenshot_path_stack.pop() | ||
|
|
||
| def _get_screenshot_paths(self, filename): | ||
| if not filename: | ||
| self._screenshot_index += 1 | ||
| filename = 'selenium-screenshot-%d.png' % self._screenshot_index | ||
| else: | ||
| filename = filename.replace('/', os.sep) | ||
| logdir = self._get_log_dir() | ||
| path = os.path.join(logdir, filename) | ||
| link = robot.utils.get_link_path(path, logdir) | ||
| screenshotDir = self._get_screenshot_directory() | ||
| path = os.path.join(screenshotDir, filename) | ||
| link = robot.utils.get_link_path(path, screenshotDir) | ||
| return path, link | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new argument needs to be documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it's on the todo list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pekkaklarck should I use backticks or single quotes? S2L isn't very consistent on its documentation style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Robot's own libs we have recently switched to use double backticks to get "code style" formatting. This is what we also recommend in Libdoc's docs:
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#representing-arguments
We used single backticks earlier but nowadays we prefer to reserve it for internal linking. Code style looks better (that's obviously subjective) and avoids accidentally creating a link.
Hmm, should I add a note about this to Robot's newly added contribution guidelines? robotframework/robotframework#1805
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please.