Skip to content

Commit 5030a95

Browse files
committed
Merge pull request robotframework#504 from aaltat/screen_shot_with_prosent_index
Use Capture Screenshot with {index} in filename.
2 parents 5a1594b + 918e9d3 commit 5030a95

File tree

2 files changed

+105
-33
lines changed

2 files changed

+105
-33
lines changed

src/Selenium2Library/keywords/_screenshot.py

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class _ScreenshotKeywords(KeywordGroup):
99

1010
def __init__(self):
11-
self._screenshot_index = 0
11+
self._screenshot_index = {}
1212
self._screenshot_path_stack = []
1313
self.screenshot_root_directory = None
1414

@@ -17,49 +17,86 @@ def __init__(self):
1717
def set_screenshot_directory(self, path, persist=False):
1818
"""Sets the root output directory for captured screenshots.
1919
20-
``path`` argument specifies the absolute path where the screenshots should
21-
be written to. If the specified ``path`` does not exist, it will be created.
22-
Setting ``persist`` specifies that the given ``path`` should
23-
be used for the rest of the test execution, otherwise the path will be restored
24-
at the end of the currently executing scope.
20+
``path`` argument specifies the absolute path where the screenshots
21+
should be written to. If the specified ``path`` does not exist,
22+
it will be created. Setting ``persist`` specifies that the given
23+
``path`` should be used for the rest of the test execution, otherwise
24+
the path will be restored at the end of the currently executing scope.
2525
"""
2626
path = os.path.abspath(path)
2727
self._create_directory(path)
2828
if persist is False:
2929
self._screenshot_path_stack.append(self.screenshot_root_directory)
3030
# Restore after current scope ends
31-
utils.events.on('scope_end', 'current', self._restore_screenshot_directory)
31+
utils.events.on('scope_end', 'current',
32+
self._restore_screenshot_directory)
3233

3334
self.screenshot_root_directory = path
3435

35-
def capture_page_screenshot(self, filename=None):
36+
def capture_page_screenshot(self,
37+
filename='selenium-screenshot-{index}.png'):
3638
"""Takes a screenshot of the current page and embeds it into the log.
3739
38-
`filename` argument specifies the name of the file to write the
39-
screenshot into. If no `filename` is given, the screenshot is saved into file
40-
`selenium-screenshot-<counter>.png` under the directory where
41-
the Robot Framework log file is written into. The `filename` is
40+
``filename`` argument specifies the name of the file to write the
41+
screenshot into. If no ``filename`` is given, the screenshot is saved
42+
into file _selenium-screenshot-{index}.png_ under the directory where
43+
the Robot Framework log file is written into. The ``filename`` is
4244
also considered relative to the same directory, if it is not
4345
given in absolute format. If an absolute or relative path is given
4446
but the path does not exist it will be created.
4547
46-
`css` can be used to modify how the screenshot is taken. By default
47-
the bakground color is changed to avoid possible problems with
48-
background leaking when the page layout is somehow broken.
48+
Starting from Selenium2Library 1.8 if ``filename`` contains _{index}_
49+
characters, it will be automatically replaced with running index.
50+
The running index is unique for each different filename. The absolute
51+
path of the saved screenshot is always returned and it does not depend
52+
does the ``filename`` contain _{index}_. See example 1 and 2 for more
53+
details.
54+
55+
The _{index}_ is replaced with the actual index by using Python's
56+
[https://docs.python.org/2/library/stdtypes.html#str.format|
57+
str.format] method, and it can be formatted using the standard
58+
[https://docs.python.org/2/library/string.html#format-string-syntax|
59+
format string syntax]. The example 3 shows this by setting the width and
60+
the fill character.
61+
62+
If there is a need to write literal _{index}_ or if ``filename``
63+
contains _{_ or _}_ characters, then the braces must be doubled.
64+
65+
Example 1:
66+
| ${file1} = | Capture Page Screenshot |
67+
| File Should Exist | ${OUTPUTDIR}${/}selenium-screenshot-1.png |
68+
| Should Be Equal | ${file1} | ${OUTPUTDIR}${/}selenium-screenshot-1.png |
69+
| ${file2} = | Capture Page Screenshot |
70+
| File Should Exist | ${OUTPUTDIR}${/}selenium-screenshot-2.png |
71+
| Should Be Equal | ${file2} | ${OUTPUTDIR}${/}selenium-screenshot-2.png |
72+
73+
Example 2:
74+
| ${file1} = | Capture Page Screenshot | ${OTHER_DIR}${/}other-{index}-name.png |
75+
| ${file2} = | Capture Page Screenshot | ${OTHER_DIR}${/}some-other-name-{index}.png |
76+
| ${file3} = | Capture Page Screenshot | ${OTHER_DIR}${/}other-{index}-name.png |
77+
| File Should Exist | ${OTHER_DIR}${/}other-1-name.png |
78+
| Should Be Equal | ${file1} | ${OTHER_DIR}${/}other-1-name.png |
79+
| File Should Exist | ${OTHER_DIR}${/}some-other-name-1.png |
80+
| Should Be Equal | ${file2} | ${OTHER_DIR}${/}some-other-name-1.png |
81+
| File Should Exist | ${OTHER_DIR}${/}other-2-name.png |
82+
| Should Be Equal | ${file3} | ${OTHER_DIR}${/}other-2-name.png |
83+
84+
Example 3:
85+
| Capture Page Screenshot | ${OTHER_DIR}${/}sc-{index:06}.png |
86+
| File Should Exist | ${OTHER_DIR}${/}sc-000001.png |
4987
"""
5088
path, link = self._get_screenshot_paths(filename)
5189
self._create_directory(path)
52-
5390
if hasattr(self._current_browser(), 'get_screenshot_as_file'):
54-
if not self._current_browser().get_screenshot_as_file(path):
55-
raise RuntimeError('Failed to save screenshot ' + filename)
91+
if not self._current_browser().get_screenshot_as_file(path):
92+
raise RuntimeError('Failed to save screenshot ' + link)
5693
else:
57-
if not self._current_browser().save_screenshot(path):
58-
raise RuntimeError('Failed to save screenshot ' + filename)
59-
94+
if not self._current_browser().save_screenshot(path):
95+
raise RuntimeError('Failed to save screenshot ' + link)
6096
# Image is shown on its own row and thus prev row is closed on purpose
6197
self._html('</td></tr><tr><td colspan="3"><a href="%s">'
6298
'<img src="%s" width="800px"></a>' % (link, link))
99+
return path
63100

64101
# Private
65102
def _create_directory(self, path):
@@ -87,14 +124,17 @@ def _restore_screenshot_directory(self):
87124
self.screenshot_root_directory = self._screenshot_path_stack.pop()
88125

89126
def _get_screenshot_paths(self, filename):
90-
if not filename:
91-
self._screenshot_index += 1
92-
filename = 'selenium-screenshot-%d.png' % self._screenshot_index
93-
else:
94-
filename = filename.replace('/', os.sep)
95-
96-
screenshotDir = self._get_screenshot_directory()
97-
logDir = self._get_log_dir()
98-
path = os.path.join(screenshotDir, filename)
99-
link = robot.utils.get_link_path(path, logDir)
127+
filename = filename.format(
128+
index=self._get_screenshot_index(filename))
129+
filename = filename.replace('/', os.sep)
130+
screenshotdir = self._get_screenshot_directory()
131+
logdir = self._get_log_dir()
132+
path = os.path.join(screenshotdir, filename)
133+
link = robot.utils.get_link_path(path, logdir)
100134
return path, link
135+
136+
def _get_screenshot_index(self, filename):
137+
if filename not in self._screenshot_index:
138+
self._screenshot_index[filename] = 0
139+
self._screenshot_index[filename] += 1
140+
return self._screenshot_index[filename]

test/acceptance/keywords/screenshots.robot

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Resource ../resource.robot
77
Capture page screenshot to default location
88
[Documentation] LOG 2:3 REGEXP: </td></tr><tr><td colspan="3"><a href="selenium-screenshot-\\d.png"><img src="selenium-screenshot-\\d.png" width="800px"></a>
99
[Setup] Remove Files ${OUTPUTDIR}/selenium-screenshot-*.png
10-
Capture Page Screenshot
10+
${file} = Capture Page Screenshot
1111
${count} = Count Files In Directory ${OUTPUTDIR} selenium-screenshot-*.png
1212
Should Be Equal As Integers ${count} 1
13+
Should Be Equal ${file} ${OUTPUTDIR}${/}selenium-screenshot-1.png
1314
Click Link Relative
1415
Wait Until Page Contains Element tag=body
1516
Capture Page Screenshot
@@ -38,11 +39,42 @@ Capture page screenshot to custom root directory
3839
[Documentation] Capture page screenshot to custom root directory
3940
[Setup] Remove Directory ${OUTPUTDIR}/custom-root recursive
4041
Set Screenshot Directory ${OUTPUTDIR}/custom-root
41-
Capture Page Screenshot custom-root-screenshot.png
42+
${file} = Capture Page Screenshot custom-root-screenshot.png
4243
File Should Exist ${OUTPUTDIR}/custom-root/custom-root-screenshot.png
44+
Should Be Equal ${file} ${OUTPUTDIR}${/}custom-root${/}custom-root-screenshot.png
4345

4446
Ensure screenshot captures revert to default root directory
4547
[Documentation] Ensure screenshot captures revert to default root directory
4648
[Setup] Remove Files ${OUTPUTDIR}/default-root-screenshot.png
4749
Capture Page Screenshot default-root-screenshot.png
4850
File Should Exist ${OUTPUTDIR}/default-root-screenshot.png
51+
52+
Capture page screenshot with unique index
53+
[Setup] Remove Directory ${OUTPUTDIR}${/}screenshot-and-index recursive
54+
${file1} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}other-{index}-name.png
55+
${file2} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-{index}.png
56+
${file3} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}other-{index}-name.png
57+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}other-1-name.png
58+
Should Be Equal ${file1} ${OUTPUTDIR}${/}screenshot-and-index${/}other-1-name.png
59+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-1.png
60+
Should Be Equal ${file2} ${OUTPUTDIR}${/}screenshot-and-index${/}some-other-name-1.png
61+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}other-2-name.png
62+
Should Be Equal ${file3} ${OUTPUTDIR}${/}screenshot-and-index${/}other-2-name.png
63+
64+
Capturing a page screenshot with two indexes should not cause an error
65+
${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}two-{index}-in-{index}-name.png
66+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}two-1-in-1-name.png
67+
Should Be Equal ${file} ${OUTPUTDIR}${/}screenshot-and-index${/}two-1-in-1-name.png
68+
69+
Capture page screenshot with index formatting
70+
Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}format-{index:06}-name.png
71+
Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}format-{index:06}-name.png
72+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}format-000001-name.png
73+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}format-000002-name.png
74+
75+
Capture page screenshot with escaped braces
76+
${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{{index}}-name.png
77+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png
78+
Should Be Equal ${file} ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png
79+
${file} = Capture Page Screenshot ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{{index-name.png
80+
File Should Exist ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index-name.png

0 commit comments

Comments
 (0)