Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def capture_screenshot(self, name):
"""Capture screenshot and save it in screenshots folder
:param name: screenshot name suffix
:returns: screenshot path
"""
from toolium.driver_wrappers_pool import DriverWrappersPool
filename = '{0:0=2d}_{1}'.format(DriverWrappersPool.screenshots_number, name)
filename = '{}.png'.format(get_valid_filename(filename))
filepath = os.path.join(DriverWrappersPool.screenshots_directory, filename)
makedirs_safe(DriverWrappersPool.screenshots_directory)
if self.driver_wrapper.driver.get_screenshot_as_file(filepath):
self.logger.info('Screenshot saved in %s', filepath)
DriverWrappersPool.screenshots_number += 1
return filepath
return None
def _download_video(self, video_url, video_name):
"""Download a video from the remote node
:param video_url: video url
:param video_name: video name
"""
from toolium.driver_wrappers_pool import DriverWrappersPool
filename = '{0:0=2d}_{1}'.format(DriverWrappersPool.videos_number, video_name)
filename = '{}.mp4'.format(get_valid_filename(filename))
filepath = os.path.join(DriverWrappersPool.videos_directory, filename)
makedirs_safe(DriverWrappersPool.videos_directory)
response = requests.get(video_url)
open(filepath, 'wb').write(response.content)
self.logger.info("Video saved in '%s'", filepath)
DriverWrappersPool.videos_number += 1
"""
# Exclude first wrapper if the driver must be reused
driver_wrappers = cls.driver_wrappers[1:] if maintain_default else cls.driver_wrappers
video_name = '{}_driver{}' if len(driver_wrappers) > 1 else '{}'
video_name = video_name if test_passed else 'error_{}'.format(video_name)
driver_index = 1
for driver_wrapper in driver_wrappers:
if not driver_wrapper.driver:
continue
try:
# Download video if necessary (error case or enabled video)
if (not test_passed or driver_wrapper.config.getboolean_optional('Server', 'video_enabled', False)) \
and driver_wrapper.remote_node_video_enabled:
if driver_wrapper.server_type in ['ggr', 'selenoid']:
name = get_valid_filename(video_name.format(name, driver_index))
Selenoid(driver_wrapper).download_session_video(name)
elif driver_wrapper.server_type == 'grid':
# Download video from Grid Extras
driver_wrapper.utils.download_remote_video(driver_wrapper.remote_node,
driver_wrapper.session_id,
video_name.format(name, driver_index))
except Exception as exc:
# Capture exceptions to avoid errors in teardown method due to session timeouts
driver_wrapper.logger.warn('Error downloading videos: %s' % exc)
driver_index += 1
def configure_visual_directories(cls, driver_info):
"""Configure screenshots, videos and visual directories
:param driver_info: driver property value to name folders
"""
if cls.screenshots_directory is None:
# Unique screenshots and videos directories
date = datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S')
folder_name = '%s_%s' % (date, driver_info) if driver_info else date
folder_name = get_valid_filename(folder_name)
cls.screenshots_directory = os.path.join(cls.output_directory, 'screenshots', folder_name)
cls.screenshots_number = 1
cls.videos_directory = os.path.join(cls.output_directory, 'videos', folder_name)
cls.logs_directory = os.path.join(cls.output_directory, 'logs', folder_name)
cls.videos_number = 1
# Unique visualtests directories
cls.visual_output_directory = os.path.join(cls.output_directory, 'visualtests', folder_name)
cls.visual_number = 1
def configure_visual_baseline(self):
"""Configure baseline directory"""
# Get baseline name
baseline_name = self.config.get_optional('VisualTests', 'baseline_name', '{Driver_type}')
for section in self.config.sections():
for option in self.config.options(section):
option_value = self.config.get(section, option)
baseline_name = baseline_name.replace('{{{0}_{1}}}'.format(section, option), option_value)
# Configure baseline directory if baseline name has changed
if self.baseline_name != baseline_name:
self.baseline_name = baseline_name
self.visual_baseline_directory = os.path.join(DriverWrappersPool.visual_baseline_directory,
get_valid_filename(baseline_name))
def save_webdriver_logs_by_type(self, log_type, test_name):
"""Get webdriver logs of the specified type and write them to a log file
:param log_type: browser, client, driver, performance, server, syslog, crashlog or logcat
:param test_name: test that has generated these logs
"""
try:
logs = self.driver_wrapper.driver.get_log(log_type)
except Exception:
return
if len(logs) > 0:
from toolium.driver_wrappers_pool import DriverWrappersPool
log_file_name = '{}_{}.txt'.format(get_valid_filename(test_name), log_type)
log_file_name = os.path.join(DriverWrappersPool.logs_directory, log_file_name)
with open(log_file_name, 'a+', encoding='utf-8') as log_file:
driver_type = self.driver_wrapper.config.get('Driver', 'type')
log_file.write(
u"\n{} '{}' test logs with driver = {}\n\n".format(datetime.now(), test_name, driver_type))
for entry in logs:
timestamp = datetime.fromtimestamp(float(entry['timestamp']) / 1000.).strftime(
'%Y-%m-%d %H:%M:%S.%f')
log_file.write(u'{}\t{}\t{}\n'.format(timestamp, entry['level'], entry['message'].rstrip()))
def save_all_ggr_logs(cls, test_name, test_passed):
"""Get all GGR logs of each driver and write them to log files
:param test_name: test that has generated these logs
:param test_passed: True if the test has passed
"""
log_name = '{} [driver {}]' if len(cls.driver_wrappers) > 1 else '{}'
driver_index = 1
for driver_wrapper in cls.driver_wrappers:
if not driver_wrapper.driver or driver_wrapper.server_type not in ['ggr', 'selenoid']:
continue
try:
if driver_wrapper.config.getboolean_optional('Server', 'logs_enabled') or not test_passed:
name = get_valid_filename(log_name.format(test_name, driver_index))
Selenoid(driver_wrapper).download_session_log(name)
except Exception as exc:
# Capture exceptions to avoid errors in teardown method due to session timeouts
driver_wrapper.logger.warn('Error downloading GGR logs: %s' % exc)
driver_index += 1