Robot Framework SeleniumLibrary
screenshot.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import os
18 
19 from robot.utils import get_link_path
20 
21 from SeleniumLibrary.base import LibraryComponent, keyword
22 from SeleniumLibrary.utils import is_noney
23 
24 
26 
27  @keyword
28 
43  def set_screenshot_directory(self, path):
44  if is_noney(path):
45  path = None
46  else:
47  path = os.path.abspath(path)
48  self._create_directory_create_directory(path)
49  previous = self.ctxctx.screenshot_root_directory
50  self.ctxctx.screenshot_root_directory = path
51  return previous
52 
53  @keyword
54 
85  def capture_page_screenshot(self, filename='selenium-screenshot-{index}.png'):
86  if not self.driversdriversdrivers.current:
87  self.infoinfo('Cannot capture screenshot because no browser is open.')
88  return
89  path = self._get_screenshot_path_get_screenshot_path(filename)
90  self._create_directory_create_directory(path)
91  if not self.driverdriverdriver.save_screenshot(path):
92  raise RuntimeError("Failed to save screenshot '{}'.".format(path))
93  self._embed_to_log_embed_to_log(path, 800)
94  return path
95 
96  @keyword
97 
115  def capture_element_screenshot(self, locator, filename='selenium-element-screenshot-{index}.png'):
116  if not self.driversdriversdrivers.current:
117  self.infoinfo('Cannot capture screenshot from element because no browser is open.')
118  return
119  path = self._get_screenshot_path_get_screenshot_path(filename)
120  self._create_directory_create_directory(path)
121  element = self.find_elementfind_element(locator, required=True)
122  if not element.screenshot(path):
123  raise RuntimeError("Failed to save element screenshot '{}'.".format(path))
124  self._embed_to_log_embed_to_log(path, 400)
125  return path
126 
127  def _get_screenshot_path(self, filename):
128  directory = self.ctxctx.screenshot_root_directory or self.log_dirlog_dirlog_dir
129  filename = filename.replace('/', os.sep)
130  index = 0
131  while True:
132  index += 1
133  formatted = filename.format(index=index)
134  path = os.path.join(directory, formatted)
135  # filename didn't contain {index} or unique path was found
136  if formatted == filename or not os.path.exists(path):
137  return path
138 
139  def _create_directory(self, path):
140  target_dir = os.path.dirname(path)
141  if not os.path.exists(target_dir):
142  os.makedirs(target_dir)
143 
144  def _embed_to_log(self, path, width):
145  # Image is shown on its own row and thus previous row is closed on
146  # purpose. Depending on Robot's log structure is a bit risky.
147  self.infoinfo('</td></tr><tr><td colspan="3">'
148  '<a href="{src}"><img src="{src}" width="{width}px"></a>'
149  .format(src=get_link_path(path, self.log_dirlog_dirlog_dir), width=width), html=True)
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
Definition: context.py:72
def capture_element_screenshot(self, locator, filename='selenium-element-screenshot-{index}.png')
Captures screenshot from the element identified by locator and embeds it into log file.
Definition: screenshot.py:115
def set_screenshot_directory(self, path)
Sets the directory for captured screenshots.
Definition: screenshot.py:43
def capture_page_screenshot(self, filename='selenium-screenshot-{index}.png')
Takes screenshot of the current page and embeds it into log file.
Definition: screenshot.py:85