From 927159f46ca5fb74697a86f1d1dc790c1ff4f765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Mon, 6 Nov 2017 12:39:35 +0200 Subject: [PATCH 1/4] Cleanup screenshot kw docs, code and tests --- src/SeleniumLibrary/__init__.py | 1 - src/SeleniumLibrary/keywords/screenshot.py | 188 +++++++-------------- test/acceptance/keywords/screenshots.robot | 54 +++--- 3 files changed, 83 insertions(+), 160 deletions(-) diff --git a/src/SeleniumLibrary/__init__.py b/src/SeleniumLibrary/__init__.py index 96aab4e78..4f60b044b 100644 --- a/src/SeleniumLibrary/__init__.py +++ b/src/SeleniumLibrary/__init__.py @@ -308,7 +308,6 @@ class SeleniumLibrary(DynamicCore): def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): - """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 2b97f4ab1..b2870e861 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -14,9 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import errno import os -import re from robot.utils import get_link_path @@ -28,13 +26,12 @@ class ScreenshotKeywords(LibraryComponent): def __init__(self, ctx): LibraryComponent.__init__(self, ctx) - self._screenshot_index = {} self._screenshot_path_stack = [] self.screenshot_root_directory = None @keyword def set_screenshot_directory(self, path, persist=False): - """Sets the root output directory for captured screenshots. + """Sets the directory for captured screenshots. ``path`` argument specifies the absolute path where the screenshots should be written to. If the specified ``path`` does not exist, @@ -51,137 +48,70 @@ def set_screenshot_directory(self, path, persist=False): self._restore_screenshot_directory) self.screenshot_root_directory = path - @keyword - def capture_page_screenshot(self, - filename='selenium-screenshot-{index}.png'): - """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 _selenium-screenshot-{index}.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. If an absolute or relative path is given - but the path does not exist it will be created. - - Starting from SeleniumLibrary 1.8 if ``filename`` contains _{index}_ - characters, it will be automatically replaced with running index. - The running index is unique for each different filename. The absolute - path of the saved screenshot is always returned and it does not depend - does the ``filename`` contain _{index}_. See example 1 and 2 for more - details. - - The _{index}_ is replaced with the actual index by using Python's - [https://docs.python.org/2/library/stdtypes.html#str.format| - str.format] method, and it can be formatted using the standard - [https://docs.python.org/2/library/string.html#format-string-syntax| - format string syntax]. The example 3 shows this by setting the width and - the fill character. - - If there is a need to write literal _{index}_ or if ``filename`` - contains _{_ or _}_ characters, then the braces must be doubled. - - If _{index}_ is used, the computed filename will not overwrite - an existing file. The number chosen will be the first number - that results in a unique filename. For example, if the - computed name is screenshot-1.png but screenshot-1.png already - exists, screenshot-2.png will be tried, and so on, until a - unique name is found. - - Example 1: - | ${file1} = | Capture Page Screenshot | - | File Should Exist | ${OUTPUTDIR}${/}selenium-screenshot-1.png | - | Should Be Equal | ${file1} | ${OUTPUTDIR}${/}selenium-screenshot-1.png | - | ${file2} = | Capture Page Screenshot | - | File Should Exist | ${OUTPUTDIR}${/}selenium-screenshot-2.png | - | Should Be Equal | ${file2} | ${OUTPUTDIR}${/}selenium-screenshot-2.png | - - Example 2: - | ${file1} = | Capture Page Screenshot | ${OTHER_DIR}${/}other-{index}-name.png | - | ${file2} = | Capture Page Screenshot | ${OTHER_DIR}${/}some-other-name-{index}.png | - | ${file3} = | Capture Page Screenshot | ${OTHER_DIR}${/}other-{index}-name.png | - | File Should Exist | ${OTHER_DIR}${/}other-1-name.png | - | Should Be Equal | ${file1} | ${OTHER_DIR}${/}other-1-name.png | - | File Should Exist | ${OTHER_DIR}${/}some-other-name-1.png | - | Should Be Equal | ${file2} | ${OTHER_DIR}${/}some-other-name-1.png | - | File Should Exist | ${OTHER_DIR}${/}other-2-name.png | - | Should Be Equal | ${file3} | ${OTHER_DIR}${/}other-2-name.png | - - Example 3: - | Capture Page Screenshot | ${OTHER_DIR}${/}sc-{index:06}.png | - | File Should Exist | ${OTHER_DIR}${/}sc-000001.png | - - """ - try: - self.browser - - except RuntimeError: - self.info("Couldn't capture page screenshot because no browser is opened") - - else: - path, link = self._get_screenshot_paths(filename) - self._create_directory(path) - if hasattr(self.browser, 'get_screenshot_as_file'): - if not self.browser.get_screenshot_as_file(path): - raise RuntimeError('Failed to save screenshot ' + link) - else: - if not self.browser.save_screenshot(path): - raise RuntimeError('Failed to save screenshot ' + link) - # Image is shown on its own row and thus prev row is closed on purpose - msg = ( - '' - ''.format(link, link) - ) - self.info(msg, html=True) - return path - 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 + os.makedirs(target_dir) - # Otherwise use RF's log directory - return self.log_dir - - # 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_template): - screenshotdir = self._get_screenshot_directory() - - filename = filename_template.format( - index=self._get_screenshot_index(filename_template)) - - # try to match {index} but not {{index}} (plus handle - # other variants like {index!r}) - if re.search(r'(?' + '' + .format(src=get_link_path(path, self.log_dir)), html=True) + return path + + def _get_screenshot_path(self, filename): + directory = self.screenshot_root_directory or self.log_dir filename = filename.replace('/', os.sep) - path = os.path.join(screenshotdir, filename) - link = get_link_path(path, self.log_dir) - return path, link - - def _get_screenshot_index(self, filename): - if filename not in self._screenshot_index: - self._screenshot_index[filename] = 0 - self._screenshot_index[filename] += 1 - return self._screenshot_index[filename] + index = 0 + while True: + index += 1 + formatted = filename.format(index=index) + path = os.path.join(directory, formatted) + # filename didn't contain {index} or unique path was found + if formatted == filename or not os.path.exists(path): + return path diff --git a/test/acceptance/keywords/screenshots.robot b/test/acceptance/keywords/screenshots.robot index c5061e1e4..8684b520c 100644 --- a/test/acceptance/keywords/screenshots.robot +++ b/test/acceptance/keywords/screenshots.robot @@ -6,7 +6,9 @@ Force Tags Known Issue Internet Explorer *** Test Cases *** Capture page screenshot to default location - [Documentation] LOG 2:3 REGEXP: + [Documentation] + ... LOG 2:3 + ... LOG 8:3 [Setup] Remove Files ${OUTPUTDIR}/selenium-screenshot-*.png ${file} = Capture Page Screenshot ${count} = Count Files In Directory ${OUTPUTDIR} selenium-screenshot-*.png @@ -19,25 +21,21 @@ Capture page screenshot to default location Should Be Equal As Integers ${count} 2 Capture page screenshot to custom file - [Documentation] Capture page screenshot to custom file [Setup] Remove Files ${OUTPUTDIR}/custom-screenshot.png Capture Page Screenshot custom-screenshot.png File Should Exist ${OUTPUTDIR}/custom-screenshot.png Capture page screenshot to custom directory - [Documentation] Capture page screenshot to custom directory [Setup] Remove Files ${TEMPDIR}/seleniumlibrary-screenshot-test.png Capture Page Screenshot ${TEMPDIR}/seleniumlibrary-screenshot-test.png File Should Exist ${TEMPDIR}/seleniumlibrary-screenshot-test.png Capture page screenshot to non-existing directory - [Documentation] Capture page screenshot to non-existing directory [Setup] Remove Directory ${OUTPUTDIR}/screenshot recursive Capture Page Screenshot screenshot/test-screenshot.png File Should Exist ${OUTPUTDIR}/screenshot/test-screenshot.png Capture page screenshot to custom root directory - [Documentation] Capture page screenshot to custom root directory [Setup] Remove Directory ${OUTPUTDIR}/custom-root recursive Set Screenshot Directory ${OUTPUTDIR}/custom-root ${file} = Capture Page Screenshot custom-root-screenshot.png @@ -45,44 +43,42 @@ Capture page screenshot to custom root directory Should Be Equal ${file} ${OUTPUTDIR}${/}custom-root${/}custom-root-screenshot.png Ensure screenshot captures revert to default root directory - [Documentation] Ensure screenshot captures revert to default root directory [Setup] Remove Files ${OUTPUTDIR}/default-root-screenshot.png Capture Page Screenshot default-root-screenshot.png File Should Exist ${OUTPUTDIR}/default-root-screenshot.png Capture page screenshot with unique index - [Setup] Remove Directory ${OUTPUTDIR}${/}screenshot-and-index recursive - ${file1} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}other-{index}-name.png - ${file2} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-{index}.png - ${file3} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}other-{index}-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}other-1-name.png + [Setup] Remove Directory ${OUTPUTDIR}/screenshot-and-index recursive + ${file1} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/other-{index}-name.png + ${file2} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/some-other-name-{index}.png + ${file3} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/other-{index}-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/other-1-name.png Should Be Equal ${file1} ${OUTPUTDIR}${/}screenshot-and-index${/}other-1-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-1.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/some-other-name-1.png Should Be Equal ${file2} ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-1.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}other-2-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/other-2-name.png Should Be Equal ${file3} ${OUTPUTDIR}${/}screenshot-and-index${/}other-2-name.png Capturing a page screenshot with two indexes should not cause an error - ${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}two-{index}-in-{index}-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}two-1-in-1-name.png + ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/two-{index}-in-{index}-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/two-1-in-1-name.png Should Be Equal ${file} ${OUTPUTDIR}${/}screenshot-and-index${/}two-1-in-1-name.png Capture page screenshot with index formatting - Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}format-{index:06}-name.png - Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}format-{index:06}-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}format-000001-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}format-000002-name.png + Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/format-{index:06}-name.png + Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/format-{index:06}-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/format-000001-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/format-000002-name.png Capture page screenshot with escaped braces - ${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{{index}}-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png + ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/brackets-{{index}}-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/brackets-{index}-name.png Should Be Equal ${file} ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png - ${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{{index-name.png - File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index-name.png + ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/brackets-{{index-name.png + File Should Exist ${OUTPUTDIR}/screenshot-and-index/brackets-{index-name.png Capture page screenshot computed name is unique - [Documentation] verify that computed filenames are unique - [Setup] run keywords + [Setup] Run Keywords ... Remove files ${OUTPUTDIR}/unique-screenshot-*.png ... AND Touch ${OUTPUTDIR}/unique-screenshot-1.png ... AND Touch ${OUTPUTDIR}/unique-screenshot-2.png @@ -102,8 +98,7 @@ Capture page screenshot computed name is unique File Should Exist ${expected} Capture page screenshot advanced formatting name is unique - [Documentation] verify that computed filenames are unique - [Setup] run keywords + [Setup] Run Keywords ... Remove files ${OUTPUTDIR}/advanced-screenshot-*.png ... AND Touch ${OUTPUTDIR}/advanced-screenshot-002.png ... AND Touch ${OUTPUTDIR}/advanced-screenshot-003.png @@ -123,8 +118,7 @@ Capture page screenshot advanced formatting name is unique File Should Exist ${expected} Capture page screenshot explicit name will overwrite - [Documentation] Verify that existing filenames get overwritten if explicitly named - [Setup] run keywords + [Setup] Run Keywords ... Remove files ${OUTPUTDIR}/explicit-screenshot-*.png ... AND Touch ${OUTPUTDIR}/explicit-screenshot-1.png # make sure we are starting out with a single file in the output directory @@ -140,7 +134,7 @@ Capture page screenshot explicit name will overwrite File Should Exist ${OUTPUTDIR}/explicit-screenshot-1.png Capture page screenshot with closed browser - [Documentation] LOG 2:1 Couldn't capture page screenshot because no browser is opened + [Documentation] LOG 2:1 Cannot capture screenshot because no browser is open. [Setup] Close All Browsers Capture Page Screenshot [Teardown] Open Browser To Start Page From dbb037abaaccb7bf8478dfc1ab36b78e467c7fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Mon, 6 Nov 2017 13:56:47 +0200 Subject: [PATCH 2/4] Fix setting screenshot directory when importing. This was a regression caused by new architecture. --- src/SeleniumLibrary/keywords/screenshot.py | 11 +++++------ test/acceptance/keywords/screenshots.robot | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index b2870e861..46e5ed075 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -26,8 +26,7 @@ class ScreenshotKeywords(LibraryComponent): def __init__(self, ctx): LibraryComponent.__init__(self, ctx) - self._screenshot_path_stack = [] - self.screenshot_root_directory = None + self._screenshot_dirs = [] @keyword def set_screenshot_directory(self, path, persist=False): @@ -42,11 +41,11 @@ def set_screenshot_directory(self, path, persist=False): path = os.path.abspath(path) self._create_directory(path) if is_falsy(persist): - self._screenshot_path_stack.append(self.screenshot_root_directory) + self._screenshot_dirs.append(self.ctx.screenshot_root_directory) # Restore after current scope ends events.on('scope_end', 'current', self._restore_screenshot_directory) - self.screenshot_root_directory = path + self.ctx.screenshot_root_directory = path def _create_directory(self, path): target_dir = os.path.dirname(path) @@ -54,7 +53,7 @@ def _create_directory(self, path): os.makedirs(target_dir) def _restore_screenshot_directory(self): - self.screenshot_root_directory = self._screenshot_path_stack.pop() + self.ctx.screenshot_root_directory = self._screenshot_dirs.pop() @keyword def capture_page_screenshot(self, @@ -105,7 +104,7 @@ def capture_page_screenshot(self, return path def _get_screenshot_path(self, filename): - directory = self.screenshot_root_directory or self.log_dir + directory = self.ctx.screenshot_root_directory or self.log_dir filename = filename.replace('/', os.sep) index = 0 while True: diff --git a/test/acceptance/keywords/screenshots.robot b/test/acceptance/keywords/screenshots.robot index 8684b520c..307a7d83f 100644 --- a/test/acceptance/keywords/screenshots.robot +++ b/test/acceptance/keywords/screenshots.robot @@ -138,3 +138,15 @@ Capture page screenshot with closed browser [Setup] Close All Browsers Capture Page Screenshot [Teardown] Open Browser To Start Page + +Set screenshot directory when importing + [Setup] Remove Files ${TEMPDIR}/selenium-screenshot-*.png + Import Library SeleniumLibrary screenshot_root_directory=${TEMPDIR} WITH NAME SL2 + Set Library Search Order SL2 + Open Browser To Start Page + Capture Page Screenshot + File Should Exist ${TEMPDIR}/selenium-screenshot-1.png + Set Screenshot Directory ${OUTPUTDIR}/screenshots + Capture Page Screenshot custom-name-{index}.png + File Should Exist ${OUTPUTDIR}/screenshots/custom-name-1.png + [Teardown] Close Browser From bc9ffb47e2221a1e2bc4a73a2a51823407be6075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Mon, 6 Nov 2017 14:37:49 +0200 Subject: [PATCH 3/4] Set Screenshot Directory changed to be consistent with other kws In practice removed the support to automatically restore the old configuration. Instead the old value is returned similarly as with other "Set ..." keywords. Fixes #985. --- src/SeleniumLibrary/keywords/screenshot.py | 47 ++++++++++--------- test/acceptance/keywords/screenshots.robot | 8 ++-- .../test_keyword_arguments_screenshot.py | 31 ------------ 3 files changed, 31 insertions(+), 55 deletions(-) delete mode 100644 test/unit/keywords/test_keyword_arguments_screenshot.py diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 46e5ed075..418b0bd87 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -19,42 +19,47 @@ from robot.utils import get_link_path from SeleniumLibrary.base import LibraryComponent, keyword -from SeleniumLibrary.utils import events, is_falsy +from SeleniumLibrary.utils import is_noney class ScreenshotKeywords(LibraryComponent): - def __init__(self, ctx): - LibraryComponent.__init__(self, ctx) - self._screenshot_dirs = [] - @keyword - def set_screenshot_directory(self, path, persist=False): + def set_screenshot_directory(self, path, persist='DEPRECATED'): """Sets the directory for captured screenshots. - ``path`` argument specifies the absolute path 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. + ``path`` argument specifies the absolute path to a directory where + the screenshots should be written to. If the directory does not + exist, it will be created. The directory can also be set when + `importing` the library. If it is not configured anywhere, + screenshots are saved to the same directory where Robot Framework's + log file is written. + + ``persist`` argument is deprecated and has no effect. + + The previous value is returned and can be used to restore + the original value later if needed. + + Deprecating ``persist`` and returning the previous value are new + in SeleniumLibrary 3.0. """ - path = os.path.abspath(path) - self._create_directory(path) - if is_falsy(persist): - self._screenshot_dirs.append(self.ctx.screenshot_root_directory) - # Restore after current scope ends - events.on('scope_end', 'current', - self._restore_screenshot_directory) + if is_noney(path): + path = None + else: + path = os.path.abspath(path) + self._create_directory(path) + if persist != 'DEPRECATED': + self.warn("'persist' argument to 'Set Screenshot Directory' " + "keyword is deprecated and has no effect.") + previous = self.ctx.screenshot_root_directory self.ctx.screenshot_root_directory = path + return previous def _create_directory(self, path): target_dir = os.path.dirname(path) if not os.path.exists(target_dir): os.makedirs(target_dir) - def _restore_screenshot_directory(self): - self.ctx.screenshot_root_directory = self._screenshot_dirs.pop() - @keyword def capture_page_screenshot(self, filename='selenium-screenshot-{index}.png'): diff --git a/test/acceptance/keywords/screenshots.robot b/test/acceptance/keywords/screenshots.robot index 307a7d83f..134290cc0 100644 --- a/test/acceptance/keywords/screenshots.robot +++ b/test/acceptance/keywords/screenshots.robot @@ -35,14 +35,16 @@ Capture page screenshot to non-existing directory Capture Page Screenshot screenshot/test-screenshot.png File Should Exist ${OUTPUTDIR}/screenshot/test-screenshot.png -Capture page screenshot to custom root directory +Set Screenshot Directory [Setup] Remove Directory ${OUTPUTDIR}/custom-root recursive - Set Screenshot Directory ${OUTPUTDIR}/custom-root + ${previous} = Set Screenshot Directory ${OUTPUTDIR}/custom-root ${file} = Capture Page Screenshot custom-root-screenshot.png File Should Exist ${OUTPUTDIR}/custom-root/custom-root-screenshot.png Should Be Equal ${file} ${OUTPUTDIR}${/}custom-root${/}custom-root-screenshot.png + [Teardown] Set Screenshot Directory ${previous} -Ensure screenshot captures revert to default root directory +Set Screenshot Directory set back to previous value + [Documentation] The directory is actually set already by previous test. [Setup] Remove Files ${OUTPUTDIR}/default-root-screenshot.png Capture Page Screenshot default-root-screenshot.png File Should Exist ${OUTPUTDIR}/default-root-screenshot.png diff --git a/test/unit/keywords/test_keyword_arguments_screenshot.py b/test/unit/keywords/test_keyword_arguments_screenshot.py deleted file mode 100644 index 75a72afd8..000000000 --- a/test/unit/keywords/test_keyword_arguments_screenshot.py +++ /dev/null @@ -1,31 +0,0 @@ -import unittest - -from mockito import mock, unstub, when, verify -from mockito.matchers import ANY - -from SeleniumLibrary.keywords import ScreenshotKeywords -from SeleniumLibrary.utils import events - - -class KeywordArgumentsElementTest(unittest.TestCase): - - def setUp(self): - ctx = mock() - ctx._browser = mock() - self.screen = ScreenshotKeywords(ctx) - self.ctx = ctx - - def tearDown(self): - unstub() - - def test_set_screenshot_directory(self): - when(events).on(ANY, ANY, ANY) - self.screen.set_screenshot_directory('/foo') - self.screen.set_screenshot_directory('/foo', None) - self.screen.set_screenshot_directory('/foo', 'None') - self.screen.set_screenshot_directory('/foo', 'False') - verify(events, times=4).on(ANY, ANY, ANY) - self.screen.set_screenshot_directory('/foo', True) - self.screen.set_screenshot_directory('/foo', 'True') - self.screen.set_screenshot_directory('/foo', 'Yes') - verify(events, times=4).on(ANY, ANY, ANY) From 556148013ffb6359ec5c9dabc0ea2b8b26bfcfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Tue, 7 Nov 2017 13:08:33 +0200 Subject: [PATCH 4/4] Moved helper after its last usage --- src/SeleniumLibrary/keywords/screenshot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 418b0bd87..2d125a9f5 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -55,11 +55,6 @@ def set_screenshot_directory(self, path, persist='DEPRECATED'): self.ctx.screenshot_root_directory = path return previous - def _create_directory(self, path): - target_dir = os.path.dirname(path) - if not os.path.exists(target_dir): - os.makedirs(target_dir) - @keyword def capture_page_screenshot(self, filename='selenium-screenshot-{index}.png'): @@ -119,3 +114,8 @@ def _get_screenshot_path(self, filename): # filename didn't contain {index} or unique path was found if formatted == filename or not os.path.exists(path): return path + + def _create_directory(self, path): + target_dir = os.path.dirname(path) + if not os.path.exists(target_dir): + os.makedirs(target_dir)