open fun call(vararg args: Any, async: Boolean = false, script: () -> String): Any?
Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.
Within the script, use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.
If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:
Arguments must be a number, a boolean, a String, WebElement, or a List
of any combination of the above. An exception will be thrown if the
arguments do not meet these criteria. The arguments will be made
available to the JavaScript via the "arguments
" magic variable, as if
the function were called via "Function.apply
"
In the case of async = true
, unlike executing synchronous JavaScript,
scripts must explicitly signal they are finished by invoking the
provided callback. This callback is always injected into the executed
function as the last argument.
The default timeout for a script to be executed is 0ms. In most cases, including the examples below, one must set the script timeout (Timeouts.setScriptTimeout) beforehand to a value sufficiently large enough.
Browser.drive(driverFactory = driverFactory) {
// When I navigate to the Kotlin's page URL
to("https://kotlinlang.org/")
// And I execute JavaScript code with arguments via `call`
val arguments = js.call(1, 2) {
"return 'Arguments: ' + arguments[0] + ', ' + arguments[1];"
}
// Then I should get the arguments as is
assertEquals(arguments, "Arguments: 1, 2")
}
args
- optional arguments that can be passed to the JS code
async
- indicates if the JS code should be executed asynchronously or not
script
- provides the JS code to be executed
See Also
org.openqa.selenium.JavascriptExecutor.executeScript
org.openqa.selenium.JavascriptExecutor.executeAsyncScript
Return
One of Boolean, Long, Double, String, List, Map or WebElement. Or null.