@@ -68,24 +68,29 @@ def confirm_action(self):
6868 def execute_javascript (self , * code ):
6969 """Executes the given JavaScript code.
7070
71- `code` may contain multiple lines of code but must contain a
72- return statement (with the value to be returned) at the end.
73-
74- `code` may be divided into multiple cells in the test data. In that
75- case, the parts are catenated together without adding spaces.
71+ `code` may contain multiple lines of code and may be divided into
72+ multiple cells in the test data. In that case, the parts are
73+ catenated together without adding spaces.
7674
7775 If `code` is an absolute path to an existing file, the JavaScript
7876 to execute will be read from that file. Forward slashes work as
7977 a path separator on all operating systems.
8078
81- Note that, by default, the code will be executed in the context of the
82- Selenium object itself, so `this` will refer to the Selenium object.
83- Use `window` to refer to the window of your application, e.g.
84- `window.document.getElementById('foo')`.
79+ The JavaScript executes in the context of the currently selected
80+ frame or window as the body of an anonymous function. Use _window_ to
81+ refer to the window of your application and _document_ to refer to the
82+ document object of the current frame or window, e.g.
83+ _document.getElementById('foo')_.
84+
85+ This keyword returns None unless there is a return statement in the
86+ JavaScript. Return values are converted to the appropriate type in
87+ Python, including WebElements.
8588
86- Example:
87- | Execute JavaScript | window.my_js_function('arg1', 'arg2') |
88- | Execute JavaScript | ${CURDIR}/js_to_execute.js |
89+ Examples:
90+ | Execute JavaScript | window.my_js_function('arg1', 'arg2') | |
91+ | Execute JavaScript | ${CURDIR}/js_to_execute.js | |
92+ | ${sum}= | Execute JavaScript | return 1 + 1; |
93+ | Should Be Equal | ${sum} | ${2} |
8994 """
9095 js = self ._get_javascript_to_execute ('' .join (code ))
9196 self ._info ("Executing JavaScript:\n %s" % js )
@@ -94,24 +99,22 @@ def execute_javascript(self, *code):
9499 def execute_async_javascript (self , * code ):
95100 """Executes asynchronous JavaScript code.
96101
97- `code` may contain multiple lines of code but must contain a
98- return statement (with the value to be returned) at the end.
102+ Similar to `Execute Javascript` except that scripts executed with
103+ this keyword must explicitly signal they are finished by invoking the
104+ provided callback. This callback is always injected into the executed
105+ function as the last argument.
99106
100- `code` may be divided into multiple cells in the test data. In that
101- case, the parts are catenated together without adding spaces .
107+ Scripts must complete within the script timeout or this keyword will
108+ fail. See the `Timeouts` section for more information .
102109
103- If `code` is an absolute path to an existing file, the JavaScript
104- to execute will be read from that file. Forward slashes work as
105- a path separator on all operating systems.
106-
107- Note that, by default, the code will be executed in the context of the
108- Selenium object itself, so `this` will refer to the Selenium object.
109- Use `window` to refer to the window of your application, e.g.
110- `window.document.getElementById('foo')`.
111-
112- Example:
113- | Execute Async JavaScript | window.my_js_function('arg1', 'arg2') |
114- | Execute Async JavaScript | ${CURDIR}/js_to_execute.js |
110+ Examples:
111+ | Execute Async JavaScript | var callback = arguments[arguments.length - 1]; | window.setTimeout(callback, 2000); |
112+ | Execute Async JavaScript | ${CURDIR}/async_js_to_execute.js | |
113+ | ${retval}= | Execute Async JavaScript | |
114+ | ... | var callback = arguments[arguments.length - 1]; | |
115+ | ... | function answer(){callback("text");}; | |
116+ | ... | window.setTimeout(answer, 2000); | |
117+ | Should Be Equal | ${retval} | text |
115118 """
116119 js = self ._get_javascript_to_execute ('' .join (code ))
117120 self ._info ("Executing Asynchronous JavaScript:\n %s" % js )
0 commit comments