def test_reset_config(): old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" resetted_val = config.reset_config("ScreenshotType") assert resetted_val == "screenshot" old_val = config.set_config("DefaultDocument", False) assert old_val is True resetted_val = config.reset_config("DefaultDocument") assert resetted_val is True old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" old_val = config.set_config("DefaultDocument", False) assert old_val is True old_val = config.set_config("CaseInsensitive", True) assert old_val is False assert config.get_config("CaseInsensitive") is True # should automatically change ContainingTextMatch assert config.get_config( "ContainingTextMatch" ) == SearchStrategies.CONTAINING_TEXT_MATCH_CASE_INSENSITIVE resetted_val = config.reset_config("CaseInsensitive") assert resetted_val is False assert config.get_config( "ContainingTextMatch" ) == SearchStrategies.CONTAINING_TEXT_MATCH_CASE_SENSITIVE config.reset_config() assert config.get_config("ScreenshotType") == "screenshot" assert config.get_config("DefaultDocument") is True
def case_insensitive(state): """*DEPRECATED!!* Use keyword `SetConfig CaseInsensitive` instead. Set containing_text_match according to selected case sensitivity. Default = False Note: if containing_text_match has been overwritten manually this will return the default value. Examples -------- .. code-block:: robotframework CaseInsensitive True CaseInsensitive False """ case_state = config.set_config("CaseInsensitive", state) if case_state: config.set_config( "ContainingTextMatch", SearchStrategies.CONTAINING_TEXT_MATCH_CASE_INSENSITIVE) else: config.set_config( "ContainingTextMatch", SearchStrategies.CONTAINING_TEXT_MATCH_CASE_SENSITIVE) return case_state
def set_search_strategy(strategy_type, xpath): """*DEPRECATED!!* Use keyword `SetConfig` instead. Set search strategy for element search. Strategy type is either "all input elements", or "matching input element". "all input elements" is a plain xpath that is used to find all elements considered as input elements. "matching input element" is an xpath with mandatory placeholder "{}" for search string. Xpath expression is completed by xpath.format(locator) internally and therefore must include placeholder "{}". Used to find elements matching with a custom search string. Placeholder can be positional, such as {0} and repeated in that case. Returns previously used search strategy. Examples -------- .. code-block:: robotframework Set search strategy active area xpath //input//textarea Set search strategy all input elements //input//textarea Set search strategy matching input element //*[@placeholder="{}"] Set search strategy matching input element containing input element ${previous}= Set search strategy all input elements //input Set search strategy all input elements ${previous} note: in the above case "containing input element" will use an xpath expression such that input elements that contain partial matches are used. Parameters ---------- xpath : str xpath expression with or without "xpath = " Raises ------ ValueError Wrong strategy type """ if strategy_type == "all input elements": previous = config.set_config("AllInputElements", xpath) elif strategy_type == "matching input element": previous = config.set_config("MatchingInputElement", xpath) elif strategy_type == 'active area xpath': previous = config.set_config("ActiveAreaXpath", xpath) elif strategy_type == 'text': previous = config.set_config("TextMatch", xpath) elif strategy_type == 'containing text': previous = config.set_config("ContainingTextMatch", xpath) else: raise ValueError("Wrong strategy type") return previous
def test_set_config(): old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" assert config.get_config("ScreenshotType") == "full_screen" assert CONFIG["ScreenshotType"] == "full_screen" old_val = config.set_config("DefaultDocument", False) assert old_val is True assert config.get_config("DefaultDocument") is False assert CONFIG["DefaultDocument"] is False
def search_from_frames(driver=None, current_frame=None): keep_frame = kwargs.get('stay_in_current_frame', CONFIG['StayInCurrentFrame']) if keep_frame: return fn(*args, **kwargs) err = None if not driver: driver = browser.get_current_browser() driver.switch_to.default_content() if current_frame: try: driver.switch_to.frame(current_frame) logger.debug('switching to childframe {}'.format(str(fn))) except (StaleElementReferenceException, WebDriverException) as e: logger.warn(e) driver.switch_to.default_content() raise e try: web_element = fn(*args, **kwargs) except QWebElementNotFoundError as e: err = e web_element = None if is_valid(web_element): return web_element start = time.time() timeout = CONFIG['FrameTimeout'] while time.time() < timeout + start: frames = fc.check_frames(driver) for frame in frames: web_element = search_from_frames(driver=driver, current_frame=frame) if is_valid(web_element): logger.debug( 'Found webelement = {}'.format(web_element)) return web_element try: driver.switch_to.parent_frame() except WebDriverException as e: driver.switch_to.default_content() raise e config.set_config('FrameTimeout', float(timeout + start - time.time())) logger.trace('Frame timeout: {}'.format(timeout)) if err: raise err return web_element driver.switch_to.default_content() raise QWebTimeoutError( 'From frame decorator: Unable to locate element in given time')
def default_document(state): """*DEPRECATED!!* Use keyword `SetConfig DefaultDocument` instead. Switches to default frame automatically. If some other frame is used by previous keyword we switch back to default after keyword is executed so that we are starting to find next locator from html document instead of previously used frame. Default = True Use False only when there is need to use and move between frames and page manually for some reason. Examples -------- .. code-block:: robotframework DefaultDocument True DefaultDocument False DefaultDocument On DefaultDocument off """ return config.set_config("DefaultDocument", state)
def css_selectors(state): """*DEPRECATED!!* Use keyword `SetConfig CssSelectors` instead. Use CSS selectors for finding elements. CSS selectors is optional way to find elements that are difficult to catch by default selectors. Typically those elements are inputs, checkboxes and dropdowns without placeholder texts. With CSS selectors the detection is tried with: * Placeholder or tooltip * Label with 'for' attribute * DOM traversing to detect sibling element Examples -------- .. code-block:: robotframework CssSelectors on TypeText MyLocator Robot CssSelectors off """ return config.set_config("CssSelectors", state)
def test_reset_config(): old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" resetted_val = config.reset_config("ScreenshotType") assert resetted_val == "screenshot" old_val = config.set_config("DefaultDocument", False) assert old_val is True resetted_val = config.reset_config("DefaultDocument") assert resetted_val is True old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" old_val = config.set_config("DefaultDocument", False) assert old_val is True config.reset_config() assert config.get_config("ScreenshotType") == "screenshot" assert config.get_config("DefaultDocument") is True
def test_set_config(): old_val = config.set_config("ScreenshotType", "full_screen") assert old_val == "screenshot" assert config.get_config("ScreenshotType") == "full_screen" assert CONFIG["ScreenshotType"] == "full_screen" old_val = config.set_config("DefaultDocument", False) assert old_val is True assert config.get_config("DefaultDocument") is False assert CONFIG["DefaultDocument"] is False old_val = config.set_config("RunBefore", "text.verify_no_text('Loading')") assert old_val == None new_kw = ["VerifyText", "Test", "timeout=10"] old_val = config.set_config("RunBefore", new_kw) assert old_val == "text.verify_no_text('Loading')" new_kw = "Verify Something" old_val = config.set_config("RunBefore", new_kw) assert old_val == ["VerifyText", "Test", "timeout=10"] assert CONFIG["RunBefore"] == "Verify Something" config.reset_config("RunBefore") assert CONFIG["RunBefore"] == None
def xhr_timeout(timeout): """*DEPRECATED!!* Use keyword `SetConfig XHRTimeout` instead. Set default timeout for XHR (How log we wait page to be loaded). Timeout can be overridden by entering it manually Examples -------- .. code-block:: robotframework XHRTimeout 60 """ return config.set_config("XHRTimeout", timeout)
def search_direction(direction): """*DEPRECATED!!* Use keyword `SetConfig SearchDirection` instead. Set search direction for element search. Search direction is "up", "down", "left", "right" and "closest" Examples -------- .. code-block:: robotframework SearchDirection right SearchDirection closest """ return config.set_config("SearchDirection", direction)
def default_timeout(timeout): """*DEPRECATED!!* Use keyword `SetConfig DefaultTimeout` instead. Set default timeout for QWeb keywords. Timeout can be overridden by entering it manually Examples -------- .. code-block:: robotframework DefaultTimeout 10s VerifyText Foo VerifyText Foo 60s """ return config.set_config("DefaultTimeout", timeout)
def screenshot_type(capture_method): """*DEPRECATED!!* Use keyword `SetConfig ScreenshotType` instead. Define how screenshot is taken. Default is normal screenshot. Html saves page as html frame in test log. All saves both image and html page. Examples -------- .. code-block:: robotframework ScreenshotType html ScreenshotType screenshot ScreenshotType all """ return config.set_config("ScreenshotType", capture_method)
def check_input_value(state): """*DEPRECATED!!* Use keyword `SetConfig CheckInputValue` instead. Check that real value matches to preferred value after TypeText. If value is not match we try to re type (three times before fail) This is optional feature. Default = false. Use with caution on elements where webdriver has tendency to lost focus and some part of the preferred text gone missing. Examples -------- .. code-block:: robotframework CheckInputValue True CheckInputValue False """ return config.set_config("CheckInputValue", state)
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
def test_non_existing_parameter(): with pytest.raises(ValueError): config.get_config("new") with pytest.raises(ValueError): config.set_config("new", 100)
def setup_function(): # we can't draw rectangle to mocked objects set_config("SearchMode", None)