Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
# By default config directory is located in test path
if not self.config_files.config_directory:
self.config_files.set_config_directory(DriverWrappersPool.get_default_config_directory())
# Get default driver wrapper and connect it
self.driver_wrapper = DriverWrappersPool.connect_default_driver_wrapper(config_files=self.config_files)
SeleniumTestCase.driver = self.driver_wrapper.driver
self.utils = self.driver_wrapper.utils
# Monkey patching assert_screenshot method in PageElement to use the correct test name
file_suffix = self.get_method_name()
def assert_screenshot_page_element(self, filename, threshold=0, exclude_elements=[], force=False):
VisualTest(self.driver_wrapper, force).assert_screenshot(self.web_element, filename, file_suffix,
threshold, exclude_elements)
PageElement.assert_screenshot = assert_screenshot_page_element
# Call BasicTestCase setUp
def setUp(self):
# Configure logger and properties
if not isinstance(self, SeleniumTestCase):
# By default config directory is located in test path
if not self.config_files.config_directory:
self.config_files.set_config_directory(DriverWrappersPool.get_default_config_directory())
self.driver_wrapper = DriverWrappersPool.get_default_wrapper()
self.config_files = DriverWrappersPool.initialize_config_files(self.config_files)
self.driver_wrapper.configure(self.config_files, is_selenium_test=False)
# Get config and logger instances
self.config = self.driver_wrapper.config
self.logger = logging.getLogger(__name__)
self.logger.info("Running new test: %s", self.get_subclassmethod_name())
def tearDownClass(cls):
# Call BasicTestCase tearDownClass
super(SeleniumTestCase, cls).tearDownClass()
# Close drivers
DriverWrappersPool.close_drivers(scope='class', test_name=cls.get_subclass_name())
def __init__(self, driver_wrapper, **kwargs):
"""
get data from properties file and session
:param driver_wrapper: driver_wrapper instance from toolium
dynamic parameters:
:param videos_dir: videos directory to download
:param logs_dir: logs directory to download
:param output_dir: general directory to download
"""
self.driver_wrapper = driver_wrapper
from toolium.driver_wrappers_pool import DriverWrappersPool
self.videos_directory = kwargs.get('videos_dir', DriverWrappersPool.videos_directory)
self.logs_directory = kwargs.get('logs_dir', DriverWrappersPool.logs_directory)
self.output_directory = kwargs.get('output_dir', DriverWrappersPool.output_directory)
self.browser_remote = driver_wrapper.config.getboolean_optional('Server', 'enabled', False)
self.browser = driver_wrapper.driver.desired_capabilities['browserName']
if self.browser_remote:
self.session_id = driver_wrapper.driver.session_id
self.server_url = driver_wrapper.utils.get_server_url()
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 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
# Get Firefox binary
firefox_binary = self.config.get_optional('Firefox', 'binary')
firefox_options = Options()
if self.config.getboolean_optional('Driver', 'headless'):
self.logger.debug("Running Firefox in headless mode")
firefox_options.add_argument('-headless')
self._add_firefox_arguments(firefox_options)
if firefox_binary:
firefox_options.binary = firefox_binary
log_path = os.path.join(DriverWrappersPool.output_directory, 'geckodriver.log')
try:
# Selenium 3
return webdriver.Firefox(firefox_profile=self._create_firefox_profile(), capabilities=capabilities,
executable_path=gecko_driver, firefox_options=firefox_options, log_path=log_path)
except TypeError:
# Selenium 2
return webdriver.Firefox(firefox_profile=self._create_firefox_profile(), capabilities=capabilities,
executable_path=gecko_driver, firefox_options=firefox_options)
def __init__(self):
if not DriverWrappersPool.is_empty():
# Copy config object and other properties from default driver
default_wrapper = DriverWrappersPool.get_default_wrapper()
self.config = default_wrapper.config.deepcopy()
self.logger = default_wrapper.logger
self.config_properties_filenames = default_wrapper.config_properties_filenames
self.config_log_filename = default_wrapper.config_log_filename
self.output_log_filename = default_wrapper.output_log_filename
self.visual_baseline_directory = default_wrapper.visual_baseline_directory
self.baseline_name = default_wrapper.baseline_name
# Create utils instance and add wrapper to the pool
self.utils = Utils(self)
DriverWrappersPool.add_wrapper(self)