Async script execution lets your test wait for a task in the browser to finish before moving on. This helps test things that take time, like loading data.
Async script execution in Selenium Java
Object result = ((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1]; window.setTimeout(function(){ callback('done'); }, 1000);"
);The last argument in the script is always a callback function you must call to finish.
executeAsyncScript waits until the callback is called or timeout happens.
Object result = ((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1]; window.setTimeout(function(){ callback('hello'); }, 500);"
);Object result = ((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1]; fetch('https://api.example.com/data').then(response => response.json()).then(data => callback(data));"
);This test opens a page, runs an async script that waits 1 second, then returns 'Async Done'. The result is printed.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class AsyncScriptExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://www.example.com"); JavascriptExecutor js = (JavascriptExecutor) driver; Object result = js.executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "window.setTimeout(function() { callback('Async Done'); }, 1000);" ); System.out.println("Result from async script: " + result); } finally { driver.quit(); } } }
Always call the callback in your async script to avoid timeout errors.
Timeout for async scripts can be set in Selenium to control max wait time.
Async scripts help test real user waits like loading or animations.
Async script execution waits for JavaScript tasks to finish before continuing.
Use executeAsyncScript with a callback to handle delayed or async browser actions.
This helps test dynamic web pages that load or change over time.
Practice
executeAsyncScript in Selenium Java?Solution
Step 1: Understand async JavaScript in Selenium
Async scripts allow waiting for tasks like AJAX calls or timers to finish before continuing.Step 2: Role of
This method runs JavaScript that signals completion via a callback, so Selenium waits properly.executeAsyncScriptFinal Answer:
To run JavaScript code that waits for asynchronous tasks to complete before returning. -> Option DQuick Check:
Async script waits for tasks = A [OK]
- Confusing async with synchronous script execution
- Thinking it pauses test for fixed time
- Assuming it runs only once at browser start
Solution
Step 1: Identify async script syntax
Async scripts require a callback function as the last argument to signal completion.Step 2: Check each option
driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);"); correctly defines a callback and calls it after 1 second delay. driver.executeAsyncScript("return 5 + 5;"); lacks callback, C uses executeScript (sync), D uses wrong syntax.Final Answer:
driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);"); -> Option AQuick Check:
Callback with setTimeout = A [OK]
- Not passing or calling the callback function
- Using executeScript instead of executeAsyncScript
- Wrong Java syntax for async scripts
Object result = driver.executeAsyncScript(
"var callback = arguments[0]; setTimeout(() => callback('done'), 500);");
System.out.println(result);Solution
Step 1: Understand the async script behavior
The script waits 500ms then calls callback with 'done'. Selenium waits for this callback and returns its value.Step 2: Check what is printed
The returned value from executeAsyncScript is 'done', so System.out.println prints 'done'.Final Answer:
done -> Option CQuick Check:
Callback returns 'done' = C [OK]
- Expecting null before callback runs
- Confusing async script with synchronous
- Assuming timeout error without delay
driver.executeAsyncScript("setTimeout(() => { return 'done'; }, 1000);");Solution
Step 1: Check async script requirements
Async scripts must accept a callback argument and call it to signal completion.Step 2: Analyze the given code
The code uses setTimeout but does not accept or call any callback, so Selenium never knows when to continue.Final Answer:
Missing callback argument and not calling it to signal completion. -> Option AQuick Check:
Callback must be called = D [OK]
- Forgetting to pass callback as argument
- Returning value instead of calling callback
- Thinking arrow functions are disallowed
driver.executeAsyncScript(
"var callback = arguments[0];
var checkData = () => {
if(document.querySelector('#data').innerText !== '') {
callback('loaded');
} else {
setTimeout(checkData, 500);
}
};
checkData();
");Solution
Step 1: Understand polling in async scripts
The script repeatedly checks if '#data' element has text, calling callback only when data is loaded.Step 2: Verify callback usage and waiting
Callback is called only after data is ready, so Selenium waits properly for dynamic content.Final Answer:
This script correctly polls for data and calls callback when ready. -> Option BQuick Check:
Polling with callback = B [OK]
- Not calling callback after data loads
- Using executeScript which does not wait
- Blocking browser with infinite loops
Ready to prove it?
Pass the test and this pattern moves unknown
