8
8
class _ScreenshotKeywords (KeywordGroup ):
9
9
10
10
def __init__ (self ):
11
- self ._screenshot_index = 0
11
+ self ._screenshot_index = {}
12
12
self ._screenshot_path_stack = []
13
13
self .screenshot_root_directory = None
14
14
@@ -17,49 +17,86 @@ def __init__(self):
17
17
def set_screenshot_directory (self , path , persist = False ):
18
18
"""Sets the root output directory for captured screenshots.
19
19
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.
25
25
"""
26
26
path = os .path .abspath (path )
27
27
self ._create_directory (path )
28
28
if persist is False :
29
29
self ._screenshot_path_stack .append (self .screenshot_root_directory )
30
30
# 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 )
32
33
33
34
self .screenshot_root_directory = path
34
35
35
- def capture_page_screenshot (self , filename = None ):
36
+ def capture_page_screenshot (self ,
37
+ filename = 'selenium-screenshot-{index}.png' ):
36
38
"""Takes a screenshot of the current page and embeds it into the log.
37
39
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
42
44
also considered relative to the same directory, if it is not
43
45
given in absolute format. If an absolute or relative path is given
44
46
but the path does not exist it will be created.
45
47
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 |
49
87
"""
50
88
path , link = self ._get_screenshot_paths (filename )
51
89
self ._create_directory (path )
52
-
53
90
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 )
56
93
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 )
60
96
# Image is shown on its own row and thus prev row is closed on purpose
61
97
self ._html ('</td></tr><tr><td colspan="3"><a href="%s">'
62
98
'<img src="%s" width="800px"></a>' % (link , link ))
99
+ return path
63
100
64
101
# Private
65
102
def _create_directory (self , path ):
@@ -87,14 +124,17 @@ def _restore_screenshot_directory(self):
87
124
self .screenshot_root_directory = self ._screenshot_path_stack .pop ()
88
125
89
126
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 )
100
134
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 ]
0 commit comments