Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import logging
import sys
import unittest
from toolium.config_driver import get_error_message_from_exception
from toolium.config_files import ConfigFiles
from toolium.driver_wrappers_pool import DriverWrappersPool
from toolium.jira import change_all_jira_status
from toolium.pageelements import PageElement
from toolium.visual_test import VisualTest
class BasicTestCase(unittest.TestCase):
"""A class whose instances are api test cases."""
config_files = ConfigFiles()
driver_wrapper = None
@classmethod
def get_subclass_name(cls):
return cls.__name__
def get_method_name(self):
# Split remove the test suffix added by ddt library
return self._testMethodName.split('___')[0]
def get_subclassmethod_name(self):
return self.__class__.__name__ + "." + self.get_method_name()
@classmethod
def tearDownClass(cls):
change_all_jira_status()
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 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)
def assert_screenshot(self, element, filename, threshold=0, exclude_elements=[], driver_wrapper=None, force=False):
"""Assert that a screenshot of an element is the same as a screenshot on disk, within a given threshold.
:param element: either a WebElement, PageElement or element locator as a tuple (locator_type, locator_value).
If None, a full screenshot is taken.
:param filename: the filename for the screenshot, which will be appended with ``.png``
:param threshold: percentage threshold for triggering a test failure (value between 0 and 1)
:param exclude_elements: list of CSS/XPATH selectors as a string or WebElement objects that must be excluded
from the assertion.
:param driver_wrapper: driver wrapper instance
:param force: if True, the screenshot is compared even if visual testing is disabled by configuration
"""
file_suffix = self.get_method_name()
VisualTest(driver_wrapper, force).assert_screenshot(element, filename, file_suffix, threshold, exclude_elements)
def assert_full_screenshot(self, filename, threshold=0, exclude_elements=[], driver_wrapper=None, force=False):
"""Assert that a driver screenshot is the same as a screenshot on disk, within a given threshold.
:param filename: the filename for the screenshot, which will be appended with ``.png``
:param threshold: percentage threshold for triggering a test failure (value between 0 and 1)
:param exclude_elements: list of CSS/XPATH selectors as a string or WebElement objects that must be excluded
from the assertion.
:param driver_wrapper: driver wrapper instance
:param force: if True, the screenshot is compared even if visual testing is disabled by configuration
"""
file_suffix = self.get_method_name()
VisualTest(driver_wrapper, force).assert_screenshot(None, filename, file_suffix, threshold, exclude_elements)
def setUp(self):
self.driver_wrapper = DriverWrappersPool.get_default_wrapper()
if not self.driver_wrapper.driver and not self.config_files.config_directory:
# By default config directory is located in test path
self.config_files.set_config_directory(DriverWrappersPool.get_default_config_directory())
super(AppiumTestCase, self).setUp()
AppiumTestCase.app_strings = self.driver_wrapper.app_strings
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 __new__(cls, driver_wrapper=None):
"""Instantiate android or ios page object from base page object depending on driver configuration
Base, Android and iOS page objects must be defined with following structure:
FOLDER/base/MODULE_NAME.py
class BasePAGE_OBJECT_NAME(MobilePageObject)
FOLDER/android/MODULE_NAME.py
class AndroidPAGE_OBJECT_NAME(BasePAGE_OBJECT_NAME)
FOLDER/ios/MODULE_NAME.py
class IosPAGE_OBJECT_NAME(BasePAGE_OBJECT_NAME)
:param driver_wrapper: driver wrapper instance
:returns: android or ios page object instance
"""
if cls.__name__.startswith('Base'):
__driver_wrapper = driver_wrapper if driver_wrapper else DriverWrappersPool.get_default_wrapper()
__os_name = 'ios' if __driver_wrapper.is_ios_test() else 'android'
__class_name = cls.__name__.replace('Base', __os_name.capitalize())
try:
return getattr(importlib.import_module(cls.__module__), __class_name)(__driver_wrapper)
except AttributeError:
__module_name = cls.__module__.replace('.base.', '.{}.'.format(__os_name))
return getattr(importlib.import_module(__module_name), __class_name)(__driver_wrapper)
else:
return super(MobilePageObject, cls).__new__(cls)