|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface JavascriptExecutor
Indicates that a driver can execute Javascript, providing access to the mechanism to do so.
Method Summary | |
---|---|
java.lang.Object |
executeAsyncScript(java.lang.String script,
java.lang.Object... args)
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. |
java.lang.Object |
executeScript(java.lang.String script,
java.lang.Object... args)
Execute javascript in the context of the currently selected frame or window. |
boolean |
isJavascriptEnabled()
It's not enough to simply support javascript, it also needs to be enabled too. |
Method Detail |
---|
java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)
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"
script
- The javascript to executeargs
- The arguments to the script. May be empty
java.lang.Object executeAsyncScript(java.lang.String script, java.lang.Object... args)
synchronous JavaScript
, scripts
executed with this method 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 first argument passed to the callback function will be uesd as the
script's result. This value will be handled as follows:
long start = System.currentTimeMillis();
((JavascriptExecutor) driver).executeAsyncScript(
"window.setTimeout(arguments[arguments.length - 1], 500);");
System.out.println(
"Elapsed time: " + System.currentTimeMillis() - start);
Example #2: Synchronizing a test with an AJAX application:
WebElement composeButton = driver.findElement(By.id("compose-button"));
composeButton.click();
((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"mailClient.getComposeWindowWidget().onload(callback);");
driver.switchTo().frame("composeWidget");
driver.findElement(By.id("to")).sendKeys("[email protected]");
Example #3: Injecting a XMLHttpRequest and waiting for the result:
Object response = ((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '/resource/data.json', true);" +
"xhr.onreadystatechange = function() {" +
" if (xhr.readyState == 4) {" +
" callback(xhr.responseText);" +
" }" +
"}" +
"xhr.send();");
JSONObject json = new JSONObject((String) response);
assertEquals("cheese", json.getString("food"));
script
- The javascript to execute.args
- The arguments to the script. May be empty.
boolean isJavascriptEnabled()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |