def perform_find(self, context_element, timeout=None): if timeout is None: timeout = WdCoreConfig.DEFAULT_TIMEOUT driver = WdCoreDriver.get_active_driver() frame_reference = WebDriverWait(driver, timeout). \ until(lambda driver: driver.find_element(self.locate_by, self.locator), message='The Element could not be located. Locator: {0} By: {1}'.format(self.locator, self.locate_by)) WdCoreDriver.get_active_driver().switch_to.frame(frame_reference) return driver
def get_parent(self): if self.parent is not None: if isinstance(self.parent, WebElement): context_element = self.parent else: context_element = self.parent.get_element() else: context_element = WdCoreDriver.get_active_driver() WdCoreDriver.reset_focus() return context_element
def drag_to(self, destination, offset=None): if offset is None: offset = (5, 5) source = self.get_element() if isinstance(destination, tuple): ActionChains( WdCoreDriver.get_active_driver()).drag_and_drop_by_offset( source, *destination).perform() else: target = destination.get_element() ActionChains(WdCoreDriver.get_active_driver()).click_and_hold(source)\ .move_to_element_with_offset(target, *offset).release().perform()
def drag_and_hold(self, destination): source = self.get_element() if isinstance(destination, tuple): ActionChains(WdCoreDriver.get_active_driver())\ .click_and_hold(source)\ .move_by_offset(*destination)\ .perform() else: target = destination.get_element() ActionChains(WdCoreDriver.get_active_driver()) \ .click_and_hold(source) \ .move_to_element(target) \ .perform()
def take_screenshot(filename=None): current_test_info = os.environ.get('PYTEST_CURRENT_TEST').split('::') # Getting this info in case we need to add it in the future test_class_name = current_test_info[1] test_name = current_test_info[2].split(' ')[0] if filename is None: filename = '{0}-{1}'.format(test_class_name, test_name) screen_shots_folder = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'screenshots') # Create the filename using the filename and the current timestamp current_time = datetime.datetime.now() filename = '{filename}_{timestamp}.png'.format( filename=filename, timestamp=current_time.strftime('%m-%d-%H%M%S')) full_file_path = os.path.join(screen_shots_folder, filename) WdCoreDriver.get_active_driver().save_screenshot(full_file_path)
def wait_for_enable_disable(self, enabled_state=True, timeout=None): if timeout is None: timeout = self.__timeout driver = WdCoreDriver.get_active_driver() if enabled_state: state = WebDriverWait( driver, timeout).until(lambda driver: self.get_element().is_enabled()) else: state = WebDriverWait(driver, timeout).until_not( lambda driver: self.get_element().is_enabled()) return state
def generate_driver(request): """ A fixture to startup the webdriver and navigate to the test site before every test. """ console_formatter = '[%(levelname)s] %(name)s %(message)s' logging.basicConfig(level=logging.NOTSET, format=console_formatter) driver = WdCoreDriver(url='http://the-internet.herokuapp.com/') def final(): """ Close driver at completion of test """ driver.driver.quit() request.addfinalizer(final) return driver
def click(self, x=None, y=None): element = self.get_element() # element.location_once_scrolled_into_view WdCoreDriver.get_active_driver().execute_script( 'return arguments[0].scrollIntoView(false);', element) start_time = time.time() while WdCoreConfig.CLICK_VISIBILITY_CHECK and not element.is_displayed( ): if time.time() - start_time > self.__timeout: raise TimeoutException( msg= "Element with identifier {0} never became visible to click." .format(self.locator)) if x is None and y is None: click_attempts = 0 while click_attempts < WdCoreConfig.ELEMENT_UNCLICKABLE_ATTEMPTS: try: element.click() break except WebDriverException as e: if not ('is not clickable at point' in e.msg) and not ( 'Unable to get element location' in e.msg): # Raise the exception if it is any other kind of error. raise e else: time.sleep(0.5) click_attempts += 1 else: element.click() else: ActionChains( WdCoreDriver.get_active_driver()).move_to_element_with_offset( element, x, y).click().perform()
def wait_for_disappearance(self, timeout=None): if timeout is None: timeout = self.__timeout context_element = self.get_parent() driver = WdCoreDriver.get_active_driver() if self.parent is not None and self.locate_by is By.XPATH and self.locator.startswith( '/'): """ Prepend the self notation for xpath if the user forgot it. The self xpath notation is required anytime a context element parent is used with xpath. """ this_locator = '.' + self.locator else: this_locator = self.locator return WebDriverWait(driver, timeout). \ until_not(lambda driver: context_element.find_element(self.locate_by, this_locator), message='The Element could not be located. Locator: {0} By: {1}'. format(self.locator, self.locate_by))
def perform_find(self, context_element, multiple=False, timeout=None): if timeout is None: timeout = self.__timeout driver = WdCoreDriver.get_active_driver() if self.parent is not None and self.locate_by is By.XPATH and self.locator.startswith( '/'): """ Prepend the self notation for xpath if the user forgot it. The self xpath notation is required anytime a context element parent is used with xpath. """ this_locator = '.' + self.locator else: this_locator = self.locator if multiple: return WebDriverWait(driver, timeout). \ until(lambda driver: context_element.find_elements(self.locate_by, this_locator), message='The Element could not be located. Locator: {0} By: {1}'. format(self.locator, self.locate_by)) else: try: el = WebDriverWait(driver, timeout). \ until(lambda driver: context_element.find_element(self.locate_by, this_locator), message='The Element could not be located. Locator: {0} By: {1}'. format(self.locator, self.locate_by)) LOGGER.debug('Element located with id: {0}'.format(el.id)) return el except (StaleElementReferenceException, WebDriverException) as exc: if WdCoreConfig.IGNORE_STALE_ELEMENTS \ and (isinstance(exc, StaleElementReferenceException) or exc.msg.startswith('unknown error: Error is not a constructor')): return self.get_element() else: raise exc
def execute_javascript(self, javascript): driver = WdCoreDriver.get_active_driver() return driver.execute_script('return arguments[0].' + javascript, self.get_element())
def mouse_over_and_click(self): element = self.get_element() ActionChains(WdCoreDriver.get_active_driver()).click_and_hold( element).release().perform()
def mouse_over(self): element = self.get_element() ActionChains(WdCoreDriver.get_active_driver()).move_to_element( element).perform()
def right_click(self): element = self.get_element() ActionChains( WdCoreDriver.get_active_driver()).context_click(element).perform()
def open_page(): WdCoreDriver(suite_config.selected_run_type['site']['url'], browser_type=suite_config.browser_type, incognito=suite_config.incognito_mode) WdCoreDriver.get_active_driver().maximize_window()
def final(): # Add teardown here WdCoreDriver.get_active_driver().quit()
def double_click(self): element = self.get_element() ActionChains( WdCoreDriver.get_active_driver()).click(element).click().perform()
def __init__(self, driver=None): if driver is None: self.driver = WdCoreDriver.get_active_driver() else: self.driver = driver