def element_text_should_not_be(self, locator, not_expected, message=None, ignore_case=False): """Verifies that element ``locator`` does not contain exact text ``not_expected``. See the `Locating elements` section for details about the locator syntax. The ``message`` argument can be used to override the default error message. The ``ignore_case`` argument can be set to True to compare case insensitive, default is False. New in SeleniumLibrary 3.1.1 """ self.info("Verifying element '%s' does not contains exact text '%s'." % (locator, not_expected)) text = self.find_element(locator).text before_not_expected = not_expected if is_truthy(ignore_case): text = text.lower() not_expected = not_expected.lower() if text == not_expected: if is_noney(message): message = ( "The text of element '%s' was not supposed to be '%s'." % (locator, before_not_expected)) raise AssertionError(message)
def element_should_contain(self, locator, expected, message=None, ignore_case=False): """Verifies that element ``locator`` contains text ``expected``. See the `Locating elements` section for details about the locator syntax. The ``message`` argument can be used to override the default error message. The ``ignore_case`` argument can be set to True to compare case insensitive, default is False. New in SeleniumLibrary 3.1. ``ignore_case`` argument new in SeleniumLibrary 3.1. Use `Element Text Should Be` if you want to match the exact text, not a substring. """ actual = actual_before = self.find_element(locator).text expected_before = expected if is_truthy(ignore_case): actual = actual.lower() expected = expected.lower() if expected not in actual: if is_noney(message): message = "Element '%s' should have contained text '%s' but "\ "its text was '%s'." % (locator, expected_before, actual_before) raise AssertionError(message) self.info("Element '%s' contains text '%s'." % (locator, expected_before))
def add_cookie(self, name, value, path=None, domain=None, secure=None, expiry=None): """Adds a cookie to your current session. ``name`` and ``value`` are required, ``path``, ``domain``, ``secure`` and ``expiry`` are optional. Expiry supports the same formats as the [http://robotframework.org/robotframework/latest/libraries/DateTime.html|DateTime] library or an epoch time stamp. Example: | `Add Cookie` | foo | bar | | | `Add Cookie` | foo | bar | domain=example.com | | `Add Cookie` | foo | bar | expiry=2027-09-28 16:21:35 | # Expiry as timestamp. | | `Add Cookie` | foo | bar | expiry=1822137695 | # Expiry as epoch seconds. | Prior to SeleniumLibrary 3.0 setting expiry did not work. """ new_cookie = {'name': name, 'value': value} if not is_noney(path): new_cookie['path'] = path if not is_noney(domain): new_cookie['domain'] = domain # Secure must be True or False if not is_noney(secure): new_cookie['secure'] = is_truthy(secure) if not is_noney(expiry): new_cookie['expiry'] = self._expiry(expiry) self.driver.add_cookie(new_cookie)
def element_text_should_be(self, locator, expected, message=None, ignore_case=False): """Verifies that element ``locator`` contains exact text ``expected``. See the `Locating elements` section for details about the locator syntax. The ``message`` argument can be used to override the default error message. The ``ignore_case`` argument can be set to True to compare case insensitive, default is False. ``ignore_case`` argument new in SeleniumLibrary 3.1. Use `Element Should Contain` if a substring match is desired. """ self.info("Verifying element '%s' contains exact text '%s'." % (locator, expected)) text = before_text = self.find_element(locator).text if is_truthy(ignore_case): text = text.lower() expected = expected.lower() if text != expected: if is_noney(message): message = ("The text of element '%s' should have been '%s' " "but it was '%s'." % (locator, expected, before_text)) raise AssertionError(message)
def element_should_not_contain(self, locator, expected, message=None, ignore_case=False): """Verifies that element ``locator`` does not contains text ``expected``. See the `Locating elements` section for details about the locator syntax. The ``message`` argument can be used to override the default error message. The ``ignore_case`` argument can be set to True to compare case insensitive, default is False. ``ignore_case`` argument new in SeleniumLibrary 3.1. """ actual = self.find_element(locator).text expected_before = expected if is_truthy(ignore_case): actual = actual.lower() expected = expected.lower() if expected in actual: if is_noney(message): message = "Element '%s' should not contain text '%s' but " \ "it did." % (locator, expected_before) raise AssertionError(message) self.info("Element '%s' does not contain text '%s'." % (locator, expected_before))
def create_chrome(self, desired_capabilities, remote_url, options=None): default = webdriver.DesiredCapabilities.CHROME if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url, options=options) capabilities = self._combine_capabilites(default, desired_capabilities) if SELENIUM_VERSION.major >= 3 and SELENIUM_VERSION.minor >= 8: return webdriver.Chrome(desired_capabilities=capabilities, options=options) return webdriver.Chrome(desired_capabilities=capabilities)
def get_alert_message(self, dismiss=True): """*DEPRECATED in SeleniumLibrary 3.2.* Use `Handle Alert` instead. Returns the message the alert has. Dismisses the alert by default (i.e. presses ``Cancel``) and setting ``dismiss`` to false leaves the alert open. There is no support to accept the alert (i.e. to press ``Ok``). `Handle Alert` has better support for controlling should the alert be accepted, dismissed, or left open. """ action = self.DISMISS if is_truthy(dismiss) else self.LEAVE return self.handle_alert(action)
def dismiss_alert(self, accept=True): """*DEPRECATED in SeleniumLibrary 3.2.* Use `Handle Alert` instead. Contrary to its name, this keyword accepts the alert by default (i.e. presses ``Ok``). ``accept`` can be set to a false value to dismiss the alert (i.e. to press ``Cancel``). `Handle Alert` has better support for controlling should the alert be accepted, dismissed, or left open. """ if is_truthy(accept): self.handle_alert(self.ACCEPT) return True self.handle_alert(self.DISMISS) return False
def create_firefox(self, desired_capabilities, remote_url, ff_profile_dir, options=None): default = webdriver.DesiredCapabilities.FIREFOX profile = self._get_ff_profile(ff_profile_dir) if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url, profile, options) capabilities = self._combine_capabilites(default, desired_capabilities) if SELENIUM_VERSION.major >= 3 and SELENIUM_VERSION.minor >= 8: return webdriver.Firefox(capabilities=capabilities, options=options, firefox_profile=profile, **self._geckodriver_log) return webdriver.Firefox(capabilities=capabilities, firefox_profile=profile, **self._geckodriver_log)
def get_list_items(self, locator, values=False): """Returns all labels or values of selection list ``locator``. See the `Locating elements` section for details about the locator syntax. Returns visible labels by default, but values can be returned by setting the ``values`` argument to a true value (see `Boolean arguments`). Example: | ${labels} = | `Get List Items` | mylist | | | ${values} = | `Get List Items` | css:#example select | values=True | Support to return values is new in SeleniumLibrary 3.0. """ options = self._get_options(locator) if is_truthy(values): return self._get_values(options) else: return self._get_labels(options)
def my_open_browser(self, url, browser='firefox', alias=None, remote_url=False, desired_capabilities=None, ff_profile_dir=None): """Opens a new browser instance to the given ``url``. The ``browser`` argument specifies which browser to use, and the supported browser are listed in the table below. The browser names are case-insensitive and some browsers have multiple supported names. | = Browser = | = Name(s) = | | Firefox | firefox, ff | | Google Chrome | googlechrome, chrome, gc | | Headless Firefox | headlessfirefox | | Headless Chrome | headlesschrome | | Internet Explorer | internetexplorer, ie | | Edge | edge | | Safari | safari | | Opera | opera | | Android | android | | Iphone | iphone | | PhantomJS | phantomjs | | HTMLUnit | htmlunit | | HTMLUnit with Javascript | htmlunitwithjs | To be able to actually use one of these browsers, you need to have a matching Selenium browser driver available. See the [https://github.com/robotframework/SeleniumLibrary#browser-drivers| project documentation] for more details. Headless Firefox and Headless Chrome are new additions in SeleniumLibrary 3.1.0 and require Selenium 3.8.0 or newer. Optional ``alias`` is an alias given for this browser instance and it can be used for switching between browsers. An alternative approach for switching is using an index returned by this keyword. These indices start from 1, are incremented when new browsers are opened, and reset back to 1 when `Close All Browsers` is called. See `Switch Browser` for more information and examples. Optional ``remote_url`` is the URL for a [https://github.com/SeleniumHQ/selenium/wiki/Grid2|Selenium Grid]. Optional ``desired_capabilities`` can be used to configure, for example, logging preferences for a browser or a browser and operating system when using [http://saucelabs.com|Sauce Labs]. Desired capabilities can be given either as a Python dictionary or as a string in format ``key1:value1,key2:value2``. [https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities| Selenium documentation] lists possible capabilities that can be enabled. Optional ``ff_profile_dir`` is the path to the Firefox profile directory if you wish to overwrite the default profile Selenium uses. Notice that prior to SeleniumLibrary 3.0, the library contained its own profile that was used by default. Examples: | `Open Browser` | http://example.com | Chrome | | `Open Browser` | http://example.com | Firefox | alias=Firefox | | `Open Browser` | http://example.com | Edge | remote_url=http://127.0.0.1:4444/wd/hub | If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more. Applying ``desired_capabilities`` argument also for local browser is new in SeleniumLibrary 3.1. """ if is_truthy(remote_url): self.info("Opening browser '%s' to base url '%s' through " "remote server at '%s'." % (browser, url, remote_url)) else: self.info("Abrindo browser '%s' no site '%s'." % (browser, url)) driver = self._make_driver(browser, desired_capabilities, ff_profile_dir, remote_url) try: driver.get(url) except Exception: self.ctx.register_driver(driver, alias) self.debug("Opened browser with session id %s but failed " "to open url '%s'." % (driver.session_id, url)) raise self.debug('Opened browser with session id %s.' % driver.session_id) return self.ctx.register_driver(driver, alias)
def create_safari(self, desired_capabilities, remote_url): default = webdriver.DesiredCapabilities.SAFARI if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url) capabilities = self._combine_capabilites(default, desired_capabilities) return webdriver.Safari(desired_capabilities=capabilities)
def create_opera(self, desired_capabilities, remote_url): default = webdriver.DesiredCapabilities.OPERA if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url) capabilities = self._combine_capabilites(default, desired_capabilities) return webdriver.Opera(desired_capabilities=capabilities)
def create_edge(self, desired_capabilities, remote_url): default = webdriver.DesiredCapabilities.EDGE if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url) capabilities = self._combine_capabilites(default, desired_capabilities) return webdriver.Edge(capabilities=capabilities)
def create_ie(self, desired_capabilities, remote_url): default = webdriver.DesiredCapabilities.INTERNETEXPLORER if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url) capabilities = self._combine_capabilites(default, desired_capabilities) return webdriver.Ie(capabilities=capabilities)
def get_matching_xpath_count(self, xpath, return_str=True): """*DEPRECATED in SeleniumLibrary 3.2.* Use `Get Element Count` instead.""" count = self.get_element_count('xpath:' + xpath) return str(count) if is_truthy(return_str) else count
def create_phantomjs(self, desired_capabilities, remote_url): default = webdriver.DesiredCapabilities.PHANTOMJS if is_truthy(remote_url): return self._remote(default, desired_capabilities, remote_url) capabilities = self._combine_capabilites(default, desired_capabilities) return webdriver.PhantomJS(desired_capabilities=capabilities)