示例#1
0
    def add_cookie(self, name, value, path=None, domain=None, secure=None):
        """Adds a cookie to your current session.

        "name" and "value" are required, "path", "domain" and "secure" are
        optional
        """
        new_cookie = {'name': name, 'value': value}
        if is_truthy(path):
            new_cookie['path'] = path
        if is_truthy(domain):
            new_cookie['domain'] = domain
        # Secure should be True or False
        if is_truthy(secure):
            new_cookie['secure'] = secure
        self.browser.add_cookie(new_cookie)
示例#2
0
    def dismiss_alert(self, accept=True):
        """ Returns true if alert was confirmed, false if it was dismissed

        This keyword will fail if no alert is present. Note that
        following keywords will fail unless the alert is
        dismissed by this keyword or another like `Get Alert Message`.
        """
        if is_truthy(accept):
            return self._handle_alert(self.ACCEPT_ALERT)
        else:
            return self._handle_alert()
 def _wait_until_no_error(self, timeout, wait_func, *args):
     timeout = timestr_to_secs(timeout) if is_truthy(
         timeout) else self.ctx._timeout_in_secs
     maxtime = time.time() + timeout
     while True:
         timeout_error = wait_func(*args)
         if not timeout_error:
             return
         if time.time() > maxtime:
             raise AssertionError(timeout_error)
         time.sleep(0.2)
示例#4
0
    def get_alert_message(self, dismiss=True):
        """Returns the text of current JavaScript alert.

        By default the current JavaScript alert will be dismissed.
        This keyword will fail if no alert is present. Note that
        following keywords will fail unless the alert is
        dismissed by this keyword or another like `Dismiss Alert`.
        """
        if is_truthy(dismiss):
            return self._handle_alert(self.DISMISS_ALERT)
        else:
            return self._handle_alert()
    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
示例#6
0
    def get_matching_xpath_count(self, xpath, return_str=True):
        """Returns number of elements matching `xpath`

        The default return type is `str` but it can changed to `int` by setting
        the ``return_str`` argument to Python False.

        One should not use the xpath= prefix for 'xpath'. XPath is assumed.

        Correct:
        | count = | Get Matching Xpath Count | //div[@id='sales-pop']
        Incorrect:
        | count = | Get Matching Xpath Count | xpath=//div[@id='sales-pop']

        If you wish to assert the number of matching elements, use
        `Xpath Should Match X Times`.
        """
        count = len(
            self.find_element("xpath=" + xpath,
                              first_only=False,
                              required=False))
        return str(count) if is_truthy(return_str) else count
    def open_browser(self,
                     url,
                     browser='firefox',
                     alias=None,
                     remote_url=False,
                     desired_capabilities=None,
                     ff_profile_dir=None):
        """Opens a new browser instance to given URL.

        Returns the index of this browser instance which can be used later to
        switch back to it. Index starts from 1 and is reset back to it when
        `Close All Browsers` keyword is used. See `Switch Browser` for
        example.

        Optional alias is an alias for the browser instance and it can be used
        for switching between browsers (just as index can be used). See `Switch
        Browser` for more details.

        Possible values for `browser` are as follows:

        | firefox          | FireFox   |
        | ff               | FireFox   |
        | internetexplorer | Internet Explorer |
        | ie               | Internet Explorer |
        | googlechrome     | Google Chrome |
        | gc               | Google Chrome |
        | chrome           | Google Chrome |
        | opera            | Opera         |
        | phantomjs        | PhantomJS     |
        | htmlunit         | HTMLUnit      |
        | htmlunitwithjs   | HTMLUnit with Javascipt support |
        | android          | Android       |
        | iphone           | Iphone        |
        | safari           | Safari        |
        | edge             | Edge          |


        Note, that you will encounter strange behavior, if you open
        multiple Internet Explorer browser instances. That is also why
        `Switch Browser` only works with one IE browser at most.
        For more information see:
        http://selenium-grid.seleniumhq.org/faq.html#i_get_some_strange_errors_when_i_run_multiple_internet_explorer_instances_on_the_same_machine

        Optional 'remote_url' is the url for a remote selenium server for example
        http://127.0.0.1:4444/wd/hub. If you specify a value for remote you can
        also specify 'desired_capabilities' which is a string in the form
        key1:val1,key2:val2 that will be used to specify desired_capabilities
        to the remote server. This is useful for doing things like specify a
        proxy server for internet explorer or for specify browser and os if your
        using saucelabs.com. 'desired_capabilities' can also be a dictonary
        (created with 'Create Dictionary') to allow for more complex configurations.

        Optional 'ff_profile_dir' is the path to the firefox profile dir if you
        wish to overwrite the default.
        """
        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("Opening browser '%s' to base url '%s'" % (browser, url))
        browser_name = browser
        browser = self._make_browser(browser_name, desired_capabilities,
                                     ff_profile_dir, remote_url)
        try:
            browser.get(url)
        except:
            self.ctx.register_browser(browser, alias)
            self.debug("Opened browser with session id %s but failed "
                       "to open url '%s'" % (browser.session_id, url))
            raise
        self.debug('Opened browser with session id %s' % browser.session_id)
        return self.ctx.register_browser(browser, alias)
 def test_is_not_truthy(self):
     not_truthys = [0, False, None, [], {}, u'', '', 'False', 'None']
     for item in not_truthys:
         self.assertFalse(is_truthy(item))
 def test_is_truthy(self):
     truthys = ['1', 'foo', ' ', 1, 23.45, True, [1, 2], 'True', {'k': 'v'}]
     for item in truthys:
         self.assertTrue(is_truthy(item))
示例#10
0
 def _format_timeout(self, timeout):
     timeout = timestr_to_secs(timeout) if is_truthy(
         timeout) else self.ctx._timeout_in_secs
     return secs_to_timestr(timeout)