def get_element_attribute(self, locator, attribute_name=None): """Returns value of the element attribute. There are two cases how to use this keyword. First, if only `locator` is provided, `locator` should consists of element locator followed by an @ sign and attribute name. This behavior is left for backward compatibility. Example: | ${id}= | Get Element Attribute | link=Link with id@id | Second, if `locator` and `attribute_name` are provided both, `locator` should be standard locator and `attribute_name` is name of the requested element attribute. Examples: | ${id}= | Get Element Attribute | link=Link with id | id | | ${element_by_dom}= | Get Webelement | dom=document.getElementsByTagName('a')[3] | | ${id}= | Get Element Attribute | ${element_by_dom} | id | """ if is_falsy(attribute_name): locator, attribute_name = self._parse_attribute_locator(locator) element = self.find_element(locator, required=False) if not element: raise ValueError("Element '%s' not found." % (locator)) return element.get_attribute(attribute_name)
def xpath_should_match_x_times(self, xpath, expected_xpath_count, message='', loglevel='INFO'): """Verifies that the page contains the given number of elements located by the given `xpath`. One should not use the xpath= prefix for 'xpath'. XPath is assumed. Correct: | Xpath Should Match X Times | //div[@id='sales-pop'] | 1 Incorrect: | Xpath Should Match X Times | xpath=//div[@id='sales-pop'] | 1 See `Page Should Contain Element` for explanation about `message` and `loglevel` arguments. """ actual_xpath_count = len( self.find_element("xpath=" + xpath, first_only=False, required=False)) if int(actual_xpath_count) != int(expected_xpath_count): if is_falsy(message): message = ("Xpath %s should have matched %s times but " "matched %s times" % (xpath, expected_xpath_count, actual_xpath_count)) self.ctx.log_source(loglevel) raise AssertionError(message) self.info("Current page contains %s elements matching '%s'." % (actual_xpath_count, xpath))
def _generic_make_browser(self, webdriver_type, desired_cap_type, remote_url, desired_caps): '''most of the make browser functions just call this function which creates the appropriate web-driver''' if is_falsy(remote_url): browser = webdriver_type() else: browser = self._create_remote_web_driver(desired_cap_type, remote_url, desired_caps) return browser
def register(self, strategy_name, strategy_keyword, persist=False): strategy = CustomLocator(self.ctx, strategy_name, strategy_keyword) if strategy.name in self._strategies: raise RuntimeError("The custom locator '%s' cannot be registered. " "A locator of that name already exists." % strategy.name) self._strategies[strategy.name] = strategy.find if is_falsy(persist): # Unregister after current scope ends events.on('scope_end', 'current', self.unregister, strategy.name)
def submit_form(self, locator=None): """Submits a form identified by `locator`. If `locator` is empty, first form in the page will be submitted. Key attributes for forms are `id` and `name`. See `introduction` for details about locating elements. """ self.info("Submitting form '%s'." % locator) if is_falsy(locator): locator = 'xpath=//form' element = self.find_element(locator, tag='form') element.submit()
def _make_ff(self, remote, desired_capabilites, profile_dir): if is_falsy(profile_dir): profile_dir = FIREFOX_PROFILE_DIR profile = webdriver.FirefoxProfile(profile_dir) if is_truthy(remote): browser = self._create_remote_web_driver( webdriver.DesiredCapabilities.FIREFOX, remote, desired_capabilites, profile) else: browser = webdriver.Firefox(firefox_profile=profile) return browser
def assert_page_contains(self, locator, tag=None, message=None, loglevel='INFO'): element_name = tag if tag else 'element' if not self.find(locator, tag, required=False): if is_falsy(message): message = ("Page should have contained %s '%s' but did not" % (element_name, locator)) self.ctx.log_source(loglevel) # TODO: Could this moved to base raise AssertionError(message) logger.info("Current page contains %s '%s'." % (element_name, locator))
def _parse_capabilities_string(self, capabilities_string): '''parses the string based desired_capabilities which should be in the form key1:val1,key2:val2 ''' desired_capabilities = {} if is_falsy(capabilities_string): return desired_capabilities for cap in capabilities_string.split(","): (key, value) = cap.split(":", 1) desired_capabilities[key.strip()] = value.strip() return desired_capabilities
def textfield_should_contain(self, locator, expected, message=''): """Verifies text field identified by `locator` contains text `expected`. `message` can be used to override default error message. Key attributes for text fields are `id` and `name`. See `introduction` for details about locating elements. """ actual = self.element_finder.get_value(locator, 'text field') if expected not in actual: if is_falsy(message): message = "Text field '%s' should have contained text '%s' "\ "but it contained '%s'" % (locator, expected, actual) raise AssertionError(message) self.info("Text field '%s' contains text '%s'." % (locator, expected))
def wait_until_page_contains(self, text, timeout=None, error=None): """Waits until `text` appears on current page. Fails if `timeout` expires before the text appears. See `introduction` for more information about `timeout` and its default value. `error` can be used to override the default error message. See also `Wait Until Page Contains Element`, `Wait For Condition`, `Wait Until Element Is Visible` and BuiltIn keyword `Wait Until Keyword Succeeds`. """ if is_falsy(error): error = "Text '%s' did not appear in <TIMEOUT>" % text self._wait_until(timeout, error, self.element.is_text_present, text)
def element_should_not_contain(self, locator, expected, message=''): """Verifies element identified by `locator` does not contain text `expected`. `message` can be used to override the default error message. Key attributes for arbitrary elements are `id` and `name`. See `Element Should Contain` for more details. """ self.info("Verifying element '%s' does not contain text '%s'." % (locator, expected)) actual = self._get_text(locator) if expected in actual: if is_falsy(message): message = "Element '%s' should not contain text '%s' but " \ "it did." % (locator, expected) raise AssertionError(message)
def element_should_not_be_visible(self, locator, message=''): """Verifies that the element identified by `locator` is NOT visible. This is the opposite of `Element Should Be Visible`. `message` can be used to override the default error message. Key attributes for arbitrary elements are `id` and `name`. See `introduction` for details about locating elements. """ self.info("Verifying element '%s' is not visible." % locator) visible = self.is_visible(locator) if visible: if is_falsy(message): message = ("The element '%s' should not be visible, " "but it is." % locator) raise AssertionError(message)
def set_screenshot_directory(self, path, persist=False): """Sets the root output directory for captured screenshots. ``path`` argument specifies the absolute path where the screenshots should be written to. If the specified ``path`` does not exist, it will be created. Setting ``persist`` specifies that the given ``path`` should be used for the rest of the test execution, otherwise the path will be restored at the end of the currently executing scope. """ path = os.path.abspath(path) self._create_directory(path) if is_falsy(persist): self._screenshot_path_stack.append(self.screenshot_root_directory) # Restore after current scope ends events.on('scope_end', 'current', self._restore_screenshot_directory) self.screenshot_root_directory = path
def textarea_value_should_be(self, locator, expected, message=''): """Verifies the value in text area identified by `locator` is exactly `expected`. `message` can be used to override default error message. Key attributes for text areas are `id` and `name`. See `introduction` for details about locating elements. """ actual = self.element_finder.get_value(locator, 'text area') if actual is not None: if expected != actual: if is_falsy(message): message = "Text field '%s' should have contained text '%s' "\ "but it contained '%s'" % (locator, expected, actual) raise AssertionError(message) else: raise ValueError("Element locator '" + locator + "' did not match any elements.") self.info("Content of text area '%s' is '%s'." % (locator, expected))
def element_should_contain(self, locator, expected, message=''): """Verifies element identified by `locator` contains text `expected`. If you wish to assert an exact (not a substring) match on the text of the element, use `Element Text Should Be`. `message` can be used to override the default error message. Key attributes for arbitrary elements are `id` and `name`. See `introduction` for details about locating elements. """ self.info("Verifying element '%s' contains " "text '%s'." % (locator, expected)) actual = self._get_text(locator) if expected not in actual: if is_falsy(message): message = "Element '%s' should have contained text '%s' but "\ "its text was '%s'." % (locator, expected, actual) raise AssertionError(message)
def textfield_value_should_be(self, locator, expected, message=''): """Verifies the value in text field identified by `locator` is exactly `expected`. `message` can be used to override default error message. Key attributes for text fields are `id` and `name`. See `introduction` for details about locating elements. """ element = self.find_element(locator, tag='text field', required=False) if not element: element = self.find_element(locator, tag='file upload', required=False) actual = element.get_attribute('value') if element else None if actual != expected: if is_falsy(message): message = "Value of text field '%s' should have been '%s' "\ "but was '%s'" % (locator, expected, actual) raise AssertionError(message) self.info("Content of text field '%s' is '%s'." % (locator, expected))
def element_should_be_visible(self, locator, message=''): """Verifies that the element identified by `locator` is visible. Herein, visible means that the element is logically visible, not optically visible in the current browser viewport. For example, an element that carries display:none is not logically visible, so using this keyword on that element would fail. `message` can be used to override the default error message. Key attributes for arbitrary elements are `id` and `name`. See `introduction` for details about locating elements. """ self.info("Verifying element '%s' is visible." % locator) visible = self.is_visible(locator) if not visible: if is_falsy(message): message = ("The element '%s' should be visible, but it " "is not." % locator) raise AssertionError(message)
def element_text_should_be(self, locator, expected, message=''): """Verifies element identified by `locator` exactly contains text `expected`. In contrast to `Element Should Contain`, this keyword does not try a substring match but an exact match on the element identified by `locator`. `message` can be used to override the default error message. Key attributes for arbitrary elements are `id` and `name`. See `introduction` for details about locating elements. """ self.info("Verifying element '%s' contains exactly text '%s'." % (locator, expected)) element = self.find_element(locator) actual = element.text if expected != actual: if is_falsy(message): message = ("The text of element '%s' should have been '%s' " "but in fact it was '%s'." % (locator, expected, actual)) raise AssertionError(message)
def wait_until_page_contains_element(self, locator, timeout=None, error=None): """Waits until element specified with `locator` appears on current page. Fails if `timeout` expires before the element appears. See `introduction` for more information about `timeout` and its default value. `error` can be used to override the default error message. See also `Wait Until Page Contains`, `Wait For Condition`, `Wait Until Element Is Visible` and BuiltIn keyword `Wait Until Keyword Succeeds`. """ def is_element_present(locator): return self.find_element(locator, required=False) is not None if is_falsy(error): error = "Element '%s' did not appear in <TIMEOUT>" % locator self._wait_until(timeout, error, is_element_present, locator)
def wait_for_condition(self, condition, timeout=None, error=None): """Waits until the given `condition` is true or `timeout` expires. The `condition` can be arbitrary JavaScript expression but must contain a return statement (with the value to be returned) at the end. See `Execute JavaScript` for information about accessing the actual contents of the window through JavaScript. `error` can be used to override the default error message. See `introduction` for more information about `timeout` and its default value. See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait Until Element Is Visible` and BuiltIn keyword `Wait Until Keyword Succeeds`. """ if is_falsy(error): error = "Condition '%s' did not become true in <TIMEOUT>" % condition self._wait_until( timeout, error, lambda: self.browser.execute_script(condition) is True)
def locator_should_match_x_times(self, locator, expected_locator_count, message='', loglevel='INFO'): """Verifies that the page contains the given number of elements located by the given `locator`. See `introduction` for details about locating elements. See `Page Should Contain Element` for explanation about `message` and `loglevel` arguments. """ actual_locator_count = len( self.find_element(locator, first_only=False, required=False)) if int(actual_locator_count) != int(expected_locator_count): if is_falsy(message): message = "Locator %s should have matched %s times but matched %s times"\ %(locator, expected_locator_count, actual_locator_count) self.ctx.log_source(loglevel) raise AssertionError(message) self.info("Current page contains %s elements matching '%s'." % (actual_locator_count, locator))
def test_is_falsy(self): truthys = ['1', 'foo', ' ', 1, 23.45, True, [1, 2], 'True', {'k': 'v'}] for item in truthys: self.assertFalse(is_falsy(item))
def test_is_not_falsy(self): not_truthys = [0, False, None, [], {}, u'', '', 'False', 'None'] for item in not_truthys: self.assertTrue(is_falsy(item))