1- import os , errno
21import robot
2+ import os , errno
3+
4+ from Selenium2Library import utils
35from keywordgroup import KeywordGroup
6+ from robot .libraries .BuiltIn import RobotNotRunningError
7+
48
59class _ScreenshotKeywords (KeywordGroup ):
610
711 def __init__ (self ):
812 self ._screenshot_index = 0
13+ self ._screenshot_path_stack = []
14+ self .screenshot_root_directory = None
915
1016 # Public
1117
18+ def set_screenshot_directory (self , path , persist = False ):
19+ """Sets the root output directory for captured screenshots.
20+
21+ ``path`` argument specifies the location to where the screenshots should
22+ be written to. If the specified ``path`` does not exist, it will be created.
23+ Setting ``persist`` specifies that the given ``path`` should
24+ be used for the rest of the test execution, otherwise the path will be restored
25+ at the end of the currently executing scope.
26+ """
27+ self ._create_directory (path )
28+ if persist is False :
29+ self ._screenshot_path_stack .append (self .screenshot_root_directory )
30+ # Restore after current scope ends
31+ utils .events .on ('scope_end' , 'current' , self ._restore_screenshot_directory )
32+
33+ self .screenshot_root_directory = path
34+
1235 def capture_page_screenshot (self , filename = None ):
1336 """Takes a screenshot of the current page and embeds it into the log.
1437
@@ -18,22 +41,14 @@ def capture_page_screenshot(self, filename=None):
1841 the Robot Framework log file is written into. The `filename` is
1942 also considered relative to the same directory, if it is not
2043 given in absolute format. If an absolute or relative path is given
21- but the path does not exist it will be created.
44+ but the path does not exist it will be created.
2245
2346 `css` can be used to modify how the screenshot is taken. By default
2447 the bakground color is changed to avoid possible problems with
2548 background leaking when the page layout is somehow broken.
2649 """
2750 path , link = self ._get_screenshot_paths (filename )
28-
29- target_dir = os .path .dirname (path )
30- if not os .path .exists (target_dir ):
31- try :
32- os .makedirs (target_dir )
33- except OSError as exc :
34- if exc .errno == errno .EEXIST and os .path .isdir (target_dir ):
35- pass
36- else : raise
51+ self ._create_directory (path )
3752
3853 if hasattr (self ._current_browser (), 'get_screenshot_as_file' ):
3954 if not self ._current_browser ().get_screenshot_as_file (path ):
@@ -47,14 +62,42 @@ def capture_page_screenshot(self, filename=None):
4762 '<img src="%s" width="800px"></a>' % (link , link ))
4863
4964 # Private
65+ def _create_directory (self , path ):
66+ target_dir = os .path .dirname (path )
67+ if not os .path .exists (target_dir ):
68+ try :
69+ os .makedirs (target_dir )
70+ except OSError as exc :
71+ if exc .errno == errno .EEXIST and os .path .isdir (target_dir ):
72+ pass
73+ else :
74+ raise
75+
76+ def _get_screenshot_directory (self ):
77+
78+ # Use screenshot root directory if set
79+ if self .screenshot_root_directory is not None :
80+ return self .screenshot_root_directory
81+
82+ # Otherwise use RF's log directory
83+ try :
84+ return self ._get_log_dir ()
85+
86+ # Unless robotframework isn't running, then use working directory
87+ except RobotNotRunningError :
88+ return os .getcwd ()
89+
90+ # should only be called by set_screenshot_directory
91+ def _restore_screenshot_directory (self ):
92+ self .screenshot_root_directory = self ._screenshot_path_stack .pop ()
5093
5194 def _get_screenshot_paths (self , filename ):
5295 if not filename :
5396 self ._screenshot_index += 1
5497 filename = 'selenium-screenshot-%d.png' % self ._screenshot_index
5598 else :
5699 filename = filename .replace ('/' , os .sep )
57- logdir = self ._get_log_dir ()
58- path = os .path .join (logdir , filename )
59- link = robot .utils .get_link_path (path , logdir )
100+ screenshotDir = self ._get_screenshot_directory ()
101+ path = os .path .join (screenshotDir , filename )
102+ link = robot .utils .get_link_path (path , screenshotDir )
60103 return path , link
0 commit comments