What if your tests could wait just the right amount of time, no more, no less?
Why Async script execution in Selenium Java? - Purpose & Use Cases
Imagine you have a web page that loads data slowly, and you want to test if a button appears after the data loads. You keep checking manually every few seconds to see if the button is there.
Manually waiting and checking is slow and boring. You might miss the exact moment the button appears, or waste time waiting too long. It's easy to make mistakes and your tests become unreliable.
Async script execution lets your test run JavaScript that waits for the page to finish loading or for certain events to happen. Your test can pause and continue exactly when ready, making it faster and more accurate.
Thread.sleep(5000); // wait fixed time
checkButtonVisible();((JavascriptExecutor)driver).executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "window.setTimeout(callback, 5000);"); checkButtonVisible();
It enables tests to wait smartly for page events, making automation faster, stable, and closer to real user experience.
Testing a shopping site where product details load after a delay; async scripts let your test wait exactly until details appear before clicking 'Add to Cart'.
Manual waiting is slow and unreliable.
Async script execution waits smartly for page events.
It makes tests faster, stable, and realistic.
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
