def click(self): try: self.parakeet.retry(method=self.click_once) except Exception as ex: LOG.error('Attempts exceeded') return self
def retry(self, method=None, **kwargs): """ Method retry the execution :param method: :param kwargs: :return: """ _next = 'next_iterator' _retry = self.retry_get_element _next_iterator = kwargs.get(_next, 1) try: LOG.debug('Trying {}/{} to perform method {}'.format( _next_iterator, _retry, method.__name__)) kwargs.pop(_next, None) result = method(**kwargs) if isinstance(result, bool) and \ not result and \ _next_iterator < _retry: return self._perform_method(_next, _next_iterator, kwargs, method) return result except Exception as ex: LOG.error('Exception: {}'.format(ex.message)) if _next_iterator < _retry: return self._perform_method(_next, _next_iterator, kwargs, method) self.selenium.save_screenshot( 'parakeet_error_{:05d}_{}.png'.format(next_image(), method.__name__)) raise ex
def _type_handle(self, _type_pause, value): if _type_pause > 0: self._type_slowly(value, _type_pause) else: LOG.debug('type_normal {}'.format(value.encode('utf-8', 'replace'))) self.element.send_keys(value)
def redirect_to_home(self): LOG.debug('redirect_to_home') WebDriverWait(self.browser.selenium, self.browser.waiting_time, self.browser.poll_frequency)\ .until(ec.title_contains(self.home_title)) WebDriverWait(self.browser.selenium, self.browser.waiting_time, self.browser.poll_frequency)\ .until(ec.invisibility_of_element_located((By.CLASS_NAME, 'main-loading'))) return self
def get_element_waiting_for_its_presence(self, locator, waiting_time=None): _waiting_time = waiting_time if waiting_time else self.waiting_time LOG.debug('get_element_waiting_for_its_presence({}, {}, {})'.format( locator, _waiting_time, self.poll_frequency)) element = WebDriverWait(self.selenium, _waiting_time, self.poll_frequency).until( ec.presence_of_element_located(locator)) return element
def get_element_waiting_for_its_presence_by_xpath(self, xpath, waiting_time=None): _waiting_time = waiting_time if waiting_time else self.waiting_time LOG.debug( 'get_element_waiting_for_its_presence_by_xpath({}, {}, {})'.format( xpath, _waiting_time, self.poll_frequency)) return self.get_element_waiting_for_its_presence((By.XPATH, xpath), _waiting_time)
def type(self, value, type_pause=0): _type_pause = type_pause if type_pause > 0 else self.parakeet.type_pause LOG.debug('type {} {}'.format(value.encode('utf-8', 'replace'), _type_pause)) self.element = self.wait_visibility_of_element_located() self._type_handle(_type_pause, value) self.debounce() return self
def fill_email(self, email): LOG.debug('fill_email') try: self.browser.find_element_by_id('identifierId').type(email) except TimeoutException: LOG.debug('click_on_identifier_before_fill_email') self.browser.find_element_by_id('identifierLink').click() self.browser.find_element_by_id('identifierId').type(email) return self
def fill_email(self, email): LOG.debug('fill_email') try: self.browser.find_element_by_id('identifierId').type(email) except TimeoutException: LOG.debug('click_on_identifier_before_fill_email') self.browser.find_element_by_xpath( '//ul/li[last()]/div[@role="link"]').click() self.browser.find_element_by_id('identifierId').type(email) return self
def login(step, user_name): """ If it is already logged do not attempt to login :param step: :param user_name: :return: """ if world.cfg['system_page_title'] not in world.browser.selenium.title: LOG.debug(world.cfg.get('login_provider','missing login provider')) if world.cfg['login_provider'] == 'google_oauth': google_oauth(user_name) elif world.cfg['login_provider'] == 'google_oauth_gapi2': google_oauth_gapi2(user_name) LoginPage(world.browser, world.cfg['system_page_title']).redirect_to_home()
def _type_slowly(self, value, type_pause): LOG.debug('type_slowly {}'.format(value.encode('utf-8', 'replace'))) if not (type_pause and isinstance(type_pause, float) and 0.0 < type_pause < 1): raise ValueError( 'The type_pause argument need to be an float valid number between 0.01 and 0.99' ) for character in value: self.element.send_keys(character) time.sleep(type_pause) return self
def get_element_waiting_for_its_presence(self, locator, waiting_time=None): _waiting_time = waiting_time if waiting_time else self.waiting_time LOG.debug('get_element_waiting_for_its_presence({}, {}, {})'.format( locator, _waiting_time, self.poll_frequency)) element = None try: element = WebDriverWait( self.selenium, _waiting_time, self.poll_frequency).until( ec.presence_of_element_located(locator)) except TimeoutException: LOG.error("Time is up! {}s".format(_waiting_time)) self.selenium\ .save_screenshot('parakeet_timeout_error_{:05d}.png' .format(next_image())) return element
def login(self): LOG.debug('login') self.browser\ .find_element_by_id('passwordNext')\ .click() return self
def fill_password(self, password): LOG.debug('fill_password') self.browser.splinter.\ is_element_visible_by_css('#password input', self.browser.waiting_time) self.browser.splinter.type('password', decode(password)) return self
def click_next(self): LOG.debug('click_next') self.browser.find_element_by_id('identifierNext').click() return self
def _remove_scroll_back(self): LOG.info('---- Remove md-scroll-mask ---') self.splinter.execute_script( "document.getElementsByClassName('md-scroll-mask')[0]['style']['display'] = 'none'" )
def quit(self): LOG.debug('quit') self.splinter.quit()
def switch_windows_after(self): LOG.debug('switch_windows_after') popup = self.browser.selenium.window_handles[1] self.browser.selenium.switch_to_window(popup) return self
def find_element_by_id(self, element_id, waiting_time=None): LOG.debug('find_element_by_id({})'.format(element_id)) locator = (By.ID, element_id) element = self.get_element_waiting_for_its_presence( locator, waiting_time) return ParakeetElement(element, locator, self)
def find_element_by_xpath(self, element_xpath): LOG.debug('find_element_by_xpath({})'.format(element_xpath)) locator = (By.XPATH, element_xpath) element = self.get_element_waiting_for_its_presence(locator) return ParakeetElement(element, locator, self)
def is_element_present_by_id(self, element_id): LOG.debug('is_element_present_by_id({})'.format(element_id)) return self.splinter.is_element_present_by_id(element_id, self.waiting_time)
def is_element_present_by_xpath(self, element_xpath, p_waiting_time=None): _waiting_time = p_waiting_time if p_waiting_time else self.waiting_time LOG.debug('is_element_present_by_xpath({}, {})'.format( element_xpath, _waiting_time)) return self.splinter.is_element_present_by_xpath( element_xpath, _waiting_time)
def is_element_present_by_xpath(self, element_xpath): LOG.debug('is_element_present_by_xpath({})'.format(element_xpath)) return self.splinter.is_element_present_by_xpath( element_xpath, self.waiting_time)
def visit_home(self): LOG.debug('visit_home') self.visit(self.config['home_url'])
def visit(self, url): LOG.debug('visit') self.splinter.visit(url)
def click_sign_in(self): LOG.debug('click_sign_in') self.browser.find_element_by_xpath('//md-card-actions/button').click() return self
def switch_windows_before(self): LOG.debug('switch_windows_before') self.browser.selenium.switch_to_window(self.window) return self
def _remove_back_drop(self): LOG.info('---- Remove md-backdrop ---') self.splinter.execute_script( "document.getElementsByTagName('md-backdrop')[0].style.display = 'none'" )
def set_window(self): LOG.debug('set_window') self.window = self.browser.selenium.window_handles[0] return self
def is_text_present(self, text): LOG.debug('is_text_present({})'.format(text)) return self.splinter.is_text_present(text)