def use_frame(locator): """Make following keywords to use frame on a page. Examples -------- .. code-block:: robotframework UseFrame //iframe Parameters ---------- locator : str Xpath expression without xpath= prefix or index (first = 1 etc.) Raises ------ NoSuchFrameException If the frame is not found """ frame.wait_page_loaded() try: index = int(locator) - 1 webelement = javascript.execute_javascript( 'document.querySelectorAll("iframe, frame")[{}]'.format(index)) except ValueError: webelement = element.get_unique_element_by_xpath(locator) driver = browser.get_current_browser() try: driver.switch_to_frame(webelement) except NoSuchFrameException: raise NoSuchFrameException('No frame wound with xpath: {0}'.format(locator))
def image_recognition(image_path, template_res_w, browser_res_w, pyautog): """Return icon's coordinates.""" image_rec = QIcon() frame.wait_page_loaded() screenshot_path = save_screenshot("screenshot.png", pyautog=pyautog) x, y = image_rec.image_location(needle=image_path, haystack=screenshot_path, template_res_w=template_res_w, device_res_w=browser_res_w) return x, y
def use_page(): """Make following keywords to use the page and not a frame on a page. Examples -------- .. code-block:: robotframework UsePage """ frame.wait_page_loaded() driver = browser.get_current_browser() driver.switch_to_default_content()
def click_header(self, text): """Click header text.""" frame.wait_page_loaded() xpath = ('//ul/li/a[.="{}"]'.format(text)) webelements = element.get_webelements_in_active_area(xpath) if not webelements: raise AssertionError( 'Could not find header with text {}'.format(text)) elif len(webelements) == 1: webelements[0].click() else: raise AssertionError( 'Found {} occurences of headers with text {}'.format( len(webelements), text))
def refresh_page(): """Refresh the current window. Examples -------- .. code-block:: robotframework RefreshPage """ frame.wait_page_loaded() driver = browser.get_current_browser() driver.refresh()
def verify_file_download(timeout=0): """Verify file has been downloaded and return file path. Parameters ---------- timeout : str | int Timeout for the download. Raises ------ ValueError Found more than one file or did not found files at all Returns ------- text : downloaded file path """ frame.wait_page_loaded() download_dir = download.get_downloads_dir() if timeout == 0: timeout = CONFIG["DefaultTimeout"] timeout_int = timestr_to_secs(timeout) start = time.time() previous_message = None while time.time() < start + timeout_int: modified_files = download.get_modified_files( download_dir, download.start_epoch) modified_files = download.remove_win_temp(modified_files) if len(modified_files) == 1: if not download.is_tmp_file(modified_files[0]): logger.info('Found downloaded file {}' .format(modified_files[0])) return modified_files[0] elif not modified_files: message = 'Could not find any modified files' if previous_message != message: logger.info(message) previous_message = message else: message = 'Modified files\n{}'.format('\n'.join(modified_files)) if previous_message != message: logger.info(message) previous_message = message if all(not download.is_tmp_file(modified_file) for modified_file in modified_files): raise ValueError('Found more than one file that was modified') time.sleep(SHORT_DELAY) raise ValueError('Could not find any modified files after {}s'.format(timeout_int))
def refresh_page(): r"""Refresh the current window. Examples -------- .. code-block:: robotframework RefreshPage Related keywords ---------------- \`Back\`, \`GoTo\`, \`MaximizeWindow\` """ frame.wait_page_loaded() driver = browser.get_current_browser() driver.refresh()
def forward(): r"""Navigates forward in the current window. Examples -------- .. code-block:: robotframework Forward Related keywords ---------------- \`Back\`, \`GoTo\`, \`RefreshPage\`, \`MaximizeWindow\` """ frame.wait_page_loaded() driver = browser.get_current_browser() driver.forward()
def get_elements_from_dom_content(*args, **kwargs): try: args, kwargs, locator = _equal_sign_handler(args, kwargs, fn) msg = None params = signature(fn).parameters args, kwargs = _args_to_kwargs(params, args, kwargs) timeout = get_timeout(**kwargs) logger.debug('Timeout is {} sec'.format(timeout)) try: if 'go_to' not in str(fn) and 'switch_window' not in str(fn): frame.wait_page_loaded() except UnexpectedAlertPresentException as e: if not CONFIG["HandleAlerts"]: raise QWebUnexpectedAlert(str(e)) logger.debug('Got {}. Trying to retry..'.format(e)) time.sleep(SHORT_DELAY) start = time.time() while time.time() < timeout + start: try: kwargs['timeout'] = float(timeout + start - time.time()) config.set_config('FrameTimeout', float(timeout + start - time.time())) return fn(*args, **kwargs) except (QWebUnexpectedConditionError, QWebTimeoutError) as e: logger.warn('Got {}'.format(e)) except (InvalidSelectorException, NoSuchElementException, QWebElementNotFoundError, UnexpectedAlertPresentException, QWebStalingElementError, StaleElementReferenceException, QWebIconNotFoundError) as e: time.sleep(SHORT_DELAY) logger.debug( 'Got exception: {}. Trying to retry..'.format(e)) except InvalidSessionIdException: CONFIG.set_value("OSScreenshots", True) raise QWebBrowserError( "Browser session lost. Did browser crash?") except (WebDriverException, QWebDriverError) as e: if any(s in str(e) for s in FATAL_MESSAGES): CONFIG.set_value("OSScreenshots", True) raise QWebBrowserError(e) logger.info( 'From timeout decorator: Webdriver exception. Retrying..' ) logger.info(e) time.sleep(SHORT_DELAY) err = QWebDriverError msg = e except QWebValueError as ve: logger.debug( 'Got QWebValueError: {}. Trying to retry..'.format(ve)) err = QWebValueError msg = ve time.sleep(SHORT_DELAY) if msg: raise err(msg) if 'count' in str(fn): return 0 if 'is_text' in str(fn) or 'is_no_text' in str(fn): return False raise QWebElementNotFoundError( 'Unable to find element for locator {} in {} sec'.format( locator, timeout)) except QWebSearchingMode: pass