can someone help me with Execute Async Javascript ? how robot understand what is the callback to wait?

696 views
Skip to first unread message

Andrea Bisello

unread,
Apr 12, 2016, 6:30:57 AM4/12/16
to robotframework-users
i'm trying to use Execute Async Javascript following the example but i'm a bit confused.

this is the example.

in the first row i see that the callback is the first parameter and the second parameter is the javascript to be executed.
in the second row i see that there is not a secondo parameter (so what is the callback?) 
and in the third row i see that 3 parameter is passed by "..." syntax... so robot will check agains "var callback" in order to understand what function is the callback?

Execute Async JavaScriptvar callback = arguments[arguments.length - 1];window.setTimeout(callback, 2000);
Execute Async JavaScript${CURDIR}/async_js_to_execute.js
${retval}=Execute Async JavaScript
...var callback = arguments[arguments.length - 1];
...function answer(){callback("text");};
...window.setTimeout(answer, 2000);
Should Be Equal${retval}text

how robot understand what is the callback to wait?

thanks.

Kevin O.

unread,
Apr 12, 2016, 2:13:19 PM4/12/16
to robotframework-users
For the keyword to pass, the script must call the callback function. That function is passed in via the arguments variable.
arguments[arguments.length - 1] is a function pointer/functor/function object for the function the script must call when it is done.
In the examples, the windows.setTimeout is dummy code. Normally, that is where you would be polling the DOM for something to change.
If the script ends without calling the callback function, the keyword fails.
If the script does not call the callback function within the time limit, the keyword fails.
I do not have a real-life example handy. Hopefully someone else here does.

Andrea Bisello

unread,
Apr 20, 2016, 12:03:15 PM4/20/16
to robotframework-users
that makes me confused is 

Similar to Execute Javascript except that scripts executed with this keyword must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

i thinked last argument of robot syntax, not last argument as the called javascript function.

in the examples, what is the position of the callback and the position of the script?
if var callback is the callback, why callback will become the last argument passed to the function? callback is always the last argument of the function by default.
why exists an example (the second) without a callback?

thanks again.

Kevin O.

unread,
Apr 20, 2016, 1:13:48 PM4/20/16
to robotframework-users
The execute JavaScript methods in Selenium support passing arguments to the script. Selenium2Library does not support passing arguments, though (yet).
The way the arguments are passed is through injecting a variable into the scope of the script named arguments.
Because there may be arguments, Selenium adds a pointer to the callback function to the end of the array.
Since S2L does not support passing arguments, arguments[arguments.length - 1] = arguments[0].
Assigning that pointer to a variable called callback just makes the script more readable.
The script could be as simple as this, though not useful:
Execute Async Javascript    arguments[arguments.length - 1]()

The second example does not have a callback because the script is hypothetical - its just to show you that you can have the script stored in a file rather than as an argument to the keyword.

The Python documentation for Selenium is pretty sparse. You should check out the Javadoc for executeAsyncScript.
Reply all
Reply to author
Forward
0 new messages