예제 #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 __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._inputs = {
            'block_until_page_ready': bool(kwargs.pop('block_until_page_ready', True)),
            'browser_breath_delay': float(kwargs.pop('browser_breath_delay', 0.05)),
            'ensure_jq': bool(kwargs.pop('ensure_jq', True)),
            'poll_frequency': float(kwargs.pop('poll_frequency', 0.2)),
        }
        self._builtin = BuiltIn()
        Selenium2Library.__init__(self, implicit_wait=implicit_wait, **kwargs)
        ExtendedElementKeywords.__init__(self)
        ExtendedFormElementKeywords.__init__(self)
        ExtendedJavascriptKeywords.__init__(self)
        ExtendedSelectElementKeywords.__init__(self)
        ExtendedWaitingKeywords.__init__(self)
        self._implicit_wait_in_secs = float(implicit_wait) if implicit_wait is not None else 15.0
        self._page_ready_keyword_list = []
        # pylint: disable=protected-access
        self._table_element_finder._element_finder = self._element_finder
예제 #3
0
 def setUp(self):
     """Instantiate the extended Javascript class."""
     self.js_code = 'return true'
     self.js_code_py = 'return True'
     self.js_code_vars = 'return ${true}'
     self.script = ExtendedJavascriptKeywords()
     self.script._current_browser = mock.Mock()
     self.script._debug = mock.Mock()
     self.script._warn = mock.Mock()
예제 #4
0
class ExtendedJavascriptTests(unittest.TestCase):
    """Extended Javascript keyword test class."""

    def setUp(self):
        """Instantiate the extended Javascript class."""
        self.js_code = 'return true'
        self.js_code_py = 'return True'
        self.js_code_vars = 'return ${true}'
        self.script = ExtendedJavascriptKeywords()
        # pylint: disable=protected-access
        self.script._current_browser = mock.Mock()
        self.script._debug = mock.Mock()
        self.script._warn = mock.Mock()

    def test_should_inherit_keywords(self):
        """Extended Javascript instance should inherit Selenium2 Javascript instances."""
        self.assertIsInstance(self.script, JavaScriptKeywords)

    def test_execute_async_js_with_replaced_vars(self):
        """Should execute async js with replaced vars."""
        # pylint: disable=protected-access
        self.script._get_javascript_to_execute = mock.Mock()
        self.script._get_javascript_to_execute.return_value = self.js_code_vars
        self.script._replace_variables_in_javascript_code = mock.Mock()
        self.script._replace_variables_in_javascript_code.return_value = self.js_code
        self.script._current_browser().execute_async_script = mock.Mock()
        self.script._current_browser().execute_async_script.return_value = True
        self.assertTrue(self.script.
                        execute_async_javascript_with_replaced_variables(self.js_code_vars))
        self.script._get_javascript_to_execute.assert_called_with(self.js_code_vars)
        self.script._replace_variables_in_javascript_code.assert_called_with(self.js_code_vars)
        self.script._debug.assert_called_with('Executing Asynchronous JavaScript:\n%s' %
                                              self.js_code)
        self.script._current_browser().execute_async_script.assert_called_with(self.js_code)

    def test_execute_js_with_replaced_vars(self):
        """Should execute js with replaced vars."""
        # pylint: disable=protected-access
        self.script._get_javascript_to_execute = mock.Mock()
        self.script._get_javascript_to_execute.return_value = self.js_code_vars
        self.script._replace_variables_in_javascript_code = mock.Mock()
        self.script._replace_variables_in_javascript_code.return_value = self.js_code
        self.script._current_browser().execute_script = mock.Mock()
        self.script._current_browser().execute_script.return_value = True
        self.assertTrue(self.script.
                        execute_javascript_with_replaced_variables(self.js_code_vars))
        self.script._get_javascript_to_execute.assert_called_with(self.js_code_vars)
        self.script._replace_variables_in_javascript_code.assert_called_with(self.js_code_vars)
        self.script._debug.assert_called_with('Executing JavaScript:\n%s' % self.js_code)
        self.script._current_browser().execute_script.assert_called_with(self.js_code)

    def test_get_screen_size(self):
        """Should return the screen size."""
        # pylint: disable=protected-access
        self.script._current_browser().execute_script = mock.Mock()
        self.script._current_browser().execute_script.return_value = [0, 0]
        self.assertEqual(self.script.get_screen_size(), [0, 0])
        self.script._current_browser().execute_script.\
            assert_called_with('return [screen.width, screen.height]')

    def test_warn_any_js_errors(self):
        """Should render warn log message for any javascript errors."""
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors()
        self.script.get_browser_logs.assert_called_with()
        # pylint: disable=protected-access
        self.script._warn.assert_called_with(' %s' % logs)

    def test_warn_any_js_errors_with_excludes(self):
        """Should render warn log message for any javascript errors with exclusion list."""
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        filtered_logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors(['miny'])
        self.script.get_browser_logs.assert_called_with()
        # pylint: disable=protected-access
        self.script._warn.assert_called_with(' %s' % filtered_logs)

    def test_warn_any_js_errors_with_label(self):
        """Should render warn log message for any javascript errors with label."""
        label = 'rhyme'
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors(label=label)
        self.script.get_browser_logs.assert_called_with()
        # pylint: disable=protected-access
        self.script._warn.assert_called_with('%s %s' % (label, logs))

    def test_replace_variables_in_js_code(self):
        """Should replace all variables in js code."""
        # pylint: disable=protected-access
        self.script._builtin = mock.Mock()
        self.script._builtin.replace_variables = mock.Mock()
        self.script._builtin.replace_variables.return_value = self.js_code_py
        self.assertEqual(self.script.
                         _replace_variables_in_javascript_code(self.js_code_vars), self.js_code)
        self.script._builtin.replace_variables.assert_called_with(self.js_code_vars)
class ExtendedJavascriptTests(unittest.TestCase):
    """Extended Javascript keyword test class."""

    def setUp(self):
        """Instantiate the extended Javascript class."""
        self.js_code = 'return true'
        self.js_code_py = 'return True'
        self.js_code_vars = 'return ${true}'
        self.script = ExtendedJavascriptKeywords()
        self.script._current_browser = mock.Mock()
        self.script._debug = mock.Mock()
        self.script._warn = mock.Mock()

    def test_should_inherit_keywords(self):
        """Extended Javascript instance should inherit Selenium2 Javascript instances."""
        self.assertIsInstance(self.script, _JavaScriptKeywords)

    def test_execute_async_js_with_replaced_vars(self):
        """Should execute async js with replaced vars."""
        self.script._get_javascript_to_execute = mock.Mock()
        self.script._get_javascript_to_execute.return_value = self.js_code_vars
        self.script._replace_variables_in_javascript_code = mock.Mock()
        self.script._replace_variables_in_javascript_code.return_value = self.js_code
        self.script._current_browser().execute_async_script = mock.Mock()
        self.script._current_browser().execute_async_script.return_value = True
        self.assertTrue(self.script.
                        execute_async_javascript_with_replaced_variables(self.js_code_vars))
        self.script._get_javascript_to_execute.assert_called_with(self.js_code_vars)
        self.script._replace_variables_in_javascript_code.assert_called_with(self.js_code_vars)
        self.script._debug.assert_called_with('Executing Asynchronous JavaScript:\n%s' %
                                              self.js_code)
        self.script._current_browser().execute_async_script.assert_called_with(self.js_code)

    def test_execute_js_with_replaced_vars(self):
        """Should execute js with replaced vars."""
        self.script._get_javascript_to_execute = mock.Mock()
        self.script._get_javascript_to_execute.return_value = self.js_code_vars
        self.script._replace_variables_in_javascript_code = mock.Mock()
        self.script._replace_variables_in_javascript_code.return_value = self.js_code
        self.script._current_browser().execute_script = mock.Mock()
        self.script._current_browser().execute_script.return_value = True
        self.assertTrue(self.script.
                        execute_javascript_with_replaced_variables(self.js_code_vars))
        self.script._get_javascript_to_execute.assert_called_with(self.js_code_vars)
        self.script._replace_variables_in_javascript_code.assert_called_with(self.js_code_vars)
        self.script._debug.assert_called_with('Executing JavaScript:\n%s' % self.js_code)
        self.script._current_browser().execute_script.assert_called_with(self.js_code)

    def test_get_screen_size(self):
        """Should return the screen size."""
        self.script._current_browser().execute_script = mock.Mock()
        self.script._current_browser().execute_script.return_value = [0, 0]
        self.assertEqual(self.script.get_screen_size(), [0, 0])
        self.script._current_browser().execute_script.\
            assert_called_with('return [screen.width, screen.height]')

    def test_warn_any_js_errors(self):
        """Should render warn log message for any javascript errors."""
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors()
        self.script.get_browser_logs.assert_called_with()
        self.script._warn.assert_called_with(' %s' % logs)

    def test_warn_any_js_errors_with_excludes(self):
        """Should render warn log message for any javascript errors with exclusion list."""
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        filtered_logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors(['miny'])
        self.script.get_browser_logs.assert_called_with()
        self.script._warn.assert_called_with(' %s' % filtered_logs)

    def test_warn_any_js_errors_with_label(self):
        """Should render warn log message for any javascript errors with label."""
        label = 'rhyme'
        logs = [{'message': 'Eeny'}, {'message': 'meeny'}, {'message': 'miny'}, {'message': 'moe'}]
        self.script.get_browser_logs = mock.Mock()
        self.script.get_browser_logs.return_value = logs
        self.script.warn_any_javascript_errors(label=label)
        self.script.get_browser_logs.assert_called_with()
        self.script._warn.assert_called_with('%s %s' % (label, logs))

    def test_replace_variables_in_js_code(self):
        """Should replace all variables in js code."""
        self.script._builtin = mock.Mock()
        self.script._builtin.replace_variables = mock.Mock()
        self.script._builtin.replace_variables.return_value = self.js_code_py
        self.assertEqual(self.script.
                         _replace_variables_in_javascript_code(self.js_code_vars), self.js_code)
        self.script._builtin.replace_variables.assert_called_with(self.js_code_vars)