コード例 #1
0
    def __init__(self, implicit_wait=15.0, **kwargs):
        # pylint: disable=line-too-long
        """ExtendedSelenium2Library can be imported with optional arguments.

        Arguments:
        - ``timeout``: The maximum value to wait for all waiting actions. (Default 5.0)
                       It can be set later with `Set Selenium Timeout`.
                       See `introduction` for more information about ``timeout``.
        - ``implicit_wait``: The maximum implicit timeout value to wait when looking
                             for elements. (Default 15.0)
                             It can be later set with `Set Selenium Implicit Wait`.
                             See [http://goo.gl/8ePMo6|WebDriver: Advanced Usage]
                             section of the SeleniumHQ documentation for more information about
                             WebDriver's implicit wait functionality.
        - ``run_on_failure``: The name of a keyword (from any available libraries) to execute
                              when a ExtendedSelenium2Library keyword fails. By default
                              `Capture Page Screenshot` will be used to take a screenshot of
                              the current page.
                              Using the value "Nothing" will disable this feature altogether.
                              See `Register Keyword To Run On Failure` keyword for
                              more information about this functionality.
        - ``screenshot_root_directory``: The default root directory that screenshots should be
                                         stored in. If not provided, the default directory will be
                                         where [http://goo.gl/lES6WM|Robot Framework] places its
                                         logfile.
        - ``block_until_page_ready``: A boolean flag to block the execution until
                                      the page is ready. (Default True)
        - ``browser_breath_delay``: The delay value in seconds to give the browser enough time to
                                    complete current execution. (Default 0.05)
        - ``ensure_jq``: A boolean flag to ensure jQuery library is loaded on the page.
                         ``sizzle`` locator strategy will depend on this flag. (Default True)
        - ``poll_frequency``: The delay value in seconds to retry the next step. (Default 0.2)

        Examples:
        | Library `|` ExtendedSelenium2Library `|` 15                                            | # Sets default timeout to 15 seconds                                       |
        | Library `|` ExtendedSelenium2Library `|` 0 `|` 5                                       | # Sets default timeout to 0 seconds and default implicit_wait to 5 seconds |
        | Library `|` ExtendedSelenium2Library `|` 5 `|` run_on_failure=Log Source               | # Sets default timeout to 5 seconds and runs `Log Source` on failure       |
        | Library `|` ExtendedSelenium2Library `|` implicit_wait=5 `|` run_on_failure=Log Source | # Sets default implicit_wait to 5 seconds and runs `Log Source` on failure |
        | Library `|` ExtendedSelenium2Library `|` timeout=10      `|` run_on_failure=Nothing    | # Sets default timeout to 10 seconds and does nothing on failure           |
        """
        # pylint: disable=line-too-long
        self._block_until_page_ready = kwargs.pop('block_until_page_ready',
                                                  True)
        self._browser_breath_delay = float(
            kwargs.pop('browser_breath_delay', 0.05))
        self._builtin = BuiltIn()
        self._ensure_jq = kwargs.pop('ensure_jq', True)
        self._poll_frequency = float(kwargs.pop('poll_frequency', 0.2))
        Selenium2Library.__init__(self, implicit_wait=implicit_wait, **kwargs)
        ExtendedJavascriptKeywords.__init__(self)
        ExtendedWaitingKeywords.__init__(self)
        self._element_finder = ExtendedElementFinder()
        self._implicit_wait_in_secs = float(
            implicit_wait) if implicit_wait is not None else 15.0
        self._page_ready_keyword_list = []
        jquery_bootstrap = self.JQUERY_BOOTSTRAP % \
            {'jquery_url': self.JQUERY_URL} if self._ensure_jq else ''
        self._page_ready_bootstrap = self.PAGE_READY_WRAPPER % \
            {'jquery_bootstrap': jquery_bootstrap}
        self._table_element_finder._element_finder = self._element_finder  # pylint: disable=protected-access
 def setUp(self):
     """Instantiate the extended element finder class."""
     self.default_strategies = ['binding', 'button', 'css', 'default', 'dom',
                                'id', 'identifier', 'jquery', 'link', 'model',
                                'name', 'options', 'partial binding',
                                'partial button', 'partial link', 'scLocator',
                                'sizzle', 'tag', 'xpath']
     self.driver = mock.Mock()
     self.driver.session_id = 'session'
     self.finder = ExtendedElementFinder()
     self.finder._filter_elements = mock.Mock()
     self.finder._find_by_css_selector = mock.Mock()
     self.finder.BUTTON_TEXT_WRAPPER = 'return buttons;%(handler)s'
     self.finder.NG_BINDING_WRAPPER = 'return bindings;%(handler)s'
     self.ng_prefixes = ['ng-', 'data-ng-', 'ng_', 'x-ng-', 'ng\\:']
     self.web_element = WebElement(self.driver, 'element', False)
     self.finder._filter_elements.return_value = self.web_element
     self.finder._find_by_css_selector.return_value = self.web_element
コード例 #3
0
 def __init__(self):
     super(ExtendedElementKeywords, self).__init__()
     self._element_finder = ExtendedElementFinder()
class ExtendedElementFinderTests(unittest.TestCase):
    """Extended element finder keyword test class."""

    def setUp(self):
        """Instantiate the extended element finder class."""
        self.default_strategies = ['binding', 'button', 'css', 'default', 'dom',
                                   'id', 'identifier', 'jquery', 'link', 'model',
                                   'name', 'options', 'partial binding',
                                   'partial button', 'partial link', 'scLocator',
                                   'sizzle', 'tag', 'xpath']
        self.driver = mock.Mock()
        self.driver.session_id = 'session'
        self.finder = ExtendedElementFinder()
        self.finder._filter_elements = mock.Mock()
        self.finder._find_by_css_selector = mock.Mock()
        self.finder.BUTTON_TEXT_WRAPPER = 'return buttons;%(handler)s'
        self.finder.NG_BINDING_WRAPPER = 'return bindings;%(handler)s'
        self.ng_prefixes = ['ng-', 'data-ng-', 'ng_', 'x-ng-', 'ng\\:']
        self.web_element = WebElement(self.driver, 'element', False)
        self.finder._filter_elements.return_value = self.web_element
        self.finder._find_by_css_selector.return_value = self.web_element

    def test_should_inherit_finder(self):
        """Extended element finder instance should inherit Selenium2 element finder instances."""
        self.assertIsInstance(self.finder, ElementFinder)

    def test_should_inherit_attributes(self):
        """Extended element finder instance should inherit its parent attributes."""
        self.assertEqual(self.finder._default_strategies, self.default_strategies)
        self.assertEqual(self.finder._ng_prefixes, self.ng_prefixes)

    def test_should_find_by_button_text(self):
        """Should find by button text."""
        button_text = 'a-button'
        constrains = 'constrains'
        script = "return buttons;return text.replace(/^\s+|\s+$/g,'')==='%s'" % \
                 button_text
        tag = 'tag'
        self.finder._find_by_button_text(self.driver, button_text, tag, constrains)
        self.driver.execute_script.assert_called_with(script)
        self.finder._filter_elements.assert_called_with(self.driver.execute_script.return_value,
                                                        tag, constrains)

    def test_should_find_by_button_text_partial(self):
        """Should find by button partial text."""
        button_text = 'a-button'
        constrains = 'constrains'
        script = "return buttons;return text.indexOf('%s')>-1" % \
                 button_text
        tag = 'tag'
        self.finder._find_by_button_text_partial(self.driver, button_text, tag, constrains)
        self.driver.execute_script.assert_called_with(script)
        self.finder._filter_elements.assert_called_with(self.driver.execute_script.return_value,
                                                        tag, constrains)

    def test_should_find_by_ng_binding(self):
        """Should find by exact binding name."""
        binding_name = 'a-binding'
        constrains = 'constrains'
        script = ("return bindings;var matcher=new RegExp('({|\\s|^|\\|)'+'%s'."
                  "replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,'\\$&')"
                  "+'(}|\\s|$|\\|)');return matcher.test(name)") % \
                  binding_name
        tag = 'tag'
        self.finder._find_by_ng_binding(self.driver, binding_name, tag, constrains)
        self.driver.execute_script.assert_called_with(script)
        self.finder._filter_elements.assert_called_with(self.driver.execute_script.return_value,
                                                        tag, constrains)

    def test_should_find_by_ng_binding_partial(self):
        """Should find by partial binding name."""
        binding_name = 'a-binding'
        constrains = 'constrains'
        script = "return bindings;return name.indexOf('%s')>-1" % \
                 binding_name
        tag = 'tag'
        self.finder._find_by_ng_binding_partial(self.driver, binding_name, tag, constrains)
        self.driver.execute_script.assert_called_with(script)
        self.finder._filter_elements.assert_called_with(self.driver.execute_script.return_value,
                                                        tag, constrains)

    def test_should_find_by_ng_model(self):
        """Should find by exact model name."""
        constrains = 'constrains'
        model_name = 'a-model'
        stem = 'model="%s"' % model_name
        joiner = '%s],[' % stem
        criteria = '[' + joiner.join(self.ng_prefixes) + stem + ']'
        tag = 'tag'
        self.finder._find_by_ng_model(self.driver, model_name, tag, constrains)
        self.finder._find_by_css_selector.assert_called_with(self.driver,
                                                             criteria, tag,
                                                             constrains)

    def test_should_find_by_ng_options(self):
        """Should find by exact descriptor."""
        constrains = 'constrains'
        descriptor = 'an-options'
        stem = 'options="%s"' % descriptor
        joiner = '%s] option,[' % stem
        criteria = '[' + joiner.join(self.ng_prefixes) + stem + '] option'
        tag = 'tag'
        self.finder._find_by_ng_options(self.driver, descriptor, tag, constrains)
        self.finder._find_by_css_selector.assert_called_with(self.driver,
                                                             criteria, tag,
                                                             constrains)