29 from PIL
import ImageGrab
36 from robot.utils import abspath, get_error_message, get_link_path
86 ROBOT_LIBRARY_SCOPE =
'TEST SUITE'
106 def __init__(self, screenshot_directory=None, screenshot_module=None):
113 elif isinstance(path, os.PathLike):
116 path = path.replace(
'/', os.sep)
117 return os.path.normpath(path)
120 _screenshot_dir = property
129 variables =
BuiltIn().get_variables()
130 outdir = variables[
'${OUTPUTDIR}']
131 log = variables[
'${LOGFILE}']
132 log = os.path.dirname(log)
if log !=
'NONE' else '.'
133 return self.
_norm_path_norm_path(os.path.join(outdir, log))
144 if not os.path.isdir(path):
145 raise RuntimeError(
"Directory '%s' does not exist." % path)
191 name = str(name)
if isinstance(name, os.PathLike)
else name.replace(
'/', os.sep)
197 logger.debug(
'Using %s module/tool for taking screenshot.'
202 logger.warn(
'Taking screenshot failed: %s\n'
203 'Make sure tests are run with a physical or virtual '
209 if not os.path.exists(os.path.dirname(path)):
210 raise RuntimeError(
"Directory '%s' where to save the screenshot "
211 "does not exist" % os.path.dirname(path))
215 if basename.lower().endswith((
'.jpg',
'.jpeg')):
221 if not os.path.exists(path):
226 logger.info(
'<a href="%s"><img src="%s" width="%s"></a>'
227 % (link, link, width), html=
True)
231 logger.info(
"Screenshot saved to '<a href=\"%s\">%s</a>'."
232 % (link, path), html=
True)
246 return self.
modulemodule !=
'no'
250 print(
"Cannot take screenshots.")
252 print(
"Using '%s' to take screenshot." % self.
modulemodule)
254 print(
"Not taking test screenshot.")
256 print(
"Taking test screenshot to '%s'." % path)
267 if sys.platform ==
'darwin':
274 screenshot_takers = {
'wxpython': (wx, self.
_wx_screenshot_wx_screenshot),
278 if name
not in screenshot_takers:
279 raise RuntimeError(
"Invalid screenshot module or tool '%s'." % name)
280 supported, screenshot_taker = screenshot_takers[name]
282 raise RuntimeError(
"Screenshot module or tool '%s' not installed."
284 return screenshot_taker
287 for module, screenshot_taker
in [(wx, self.
_wx_screenshot_wx_screenshot),
293 return screenshot_taker
296 if self.
_call_call(
'screencapture',
'-t',
'jpg', path) != 0:
301 return subprocess.call(command, stdout=subprocess.PIPE,
302 stderr=subprocess.STDOUT)
310 return os.sep ==
'/' and self.
_call_call(
'scrot',
'--version') == 0
313 if not path.endswith((
'.jpg',
'.jpeg')):
314 raise RuntimeError(
"Scrot requires extension to be '.jpg' or "
315 "'.jpeg', got '%s'." % os.path.splitext(path)[1])
316 if os.path.exists(path):
318 if self.
_call_call(
'scrot',
'--silent', path) != 0:
324 context = wx.ScreenDC()
325 width, height = context.GetSize()
326 if wx.__version__ >=
'4':
327 bitmap = wx.Bitmap(width, height, -1)
329 bitmap = wx.EmptyBitmap(width, height, -1)
330 memory = wx.MemoryDC()
331 memory.SelectObject(bitmap)
332 memory.Blit(0, 0, width, height, context, -1, -1)
333 memory.SelectObject(wx.NullBitmap)
334 bitmap.SaveFile(path, wx.BITMAP_TYPE_JPEG)
337 window = gdk.get_default_root_window()
340 width, height = window.get_size()
341 pb = gdk.Pixbuf(gdk.COLORSPACE_RGB,
False, 8, width, height)
342 pb = pb.get_from_drawable(window, window.get_colormap(),
343 0, 0, 0, 0, width, height)
346 pb.save(path,
'jpeg')
349 ImageGrab.grab().save(path,
'JPEG')
352 raise RuntimeError(
'Taking screenshots is not supported on this platform '
353 'by default. See library documentation for details.')
356 if __name__ ==
"__main__":
357 if len(sys.argv)
not in [2, 3]:
358 sys.exit(
"Usage: %s <path>|test [wxpython|pygtk|pil|scrot]"
359 % os.path.basename(sys.argv[0]))
360 path = sys.argv[1]
if sys.argv[1] !=
'test' else None
361 module = sys.argv[2]
if len(sys.argv) > 2
else None
An always available standard library with often needed keywords.
def _get_screenshot_taker(self, module_name=None)
def _call(self, *command)
def _scrot_screenshot(self, path)
def _osx_screenshot(self, path)
def _no_screenshot(self, path)
def test(self, path=None)
def _get_named_screenshot_taker(self, name)
def _pil_screenshot(self, path)
def _get_default_screenshot_taker(self)
def __init__(self, module_name=None)
def _wx_screenshot(self, path)
def _gtk_screenshot(self, path)
Library for taking screenshots on the machine where tests are executed.
def __init__(self, screenshot_directory=None, screenshot_module=None)
Configure where screenshots are saved.
def _link_screenshot(self, path)
def _screenshot_dir(self)
def _save_screenshot(self, name)
def _validate_screenshot_path(self, path)
def _embed_screenshot(self, path, width)
def _get_screenshot_path(self, basename)
def _norm_path(self, path)
def take_screenshot_without_embedding(self, name="screenshot")
Takes a screenshot and links it from the log file.
def _screenshot_to_file(self, path)
def take_screenshot(self, name="screenshot", width="800px")
Takes a screenshot in JPEG format and embeds it into the log file.
def set_screenshot_directory(self, path)
Sets the directory where screenshots are saved.
def get_error_message()
Returns error message of the last occurred exception.
def abspath(path, case_normalize=False)
Replacement for os.path.abspath with some enhancements and bug fixes.
def get_link_path(target, base)
Returns a relative path to target from base.
def get_version(naked=False)