Challenge - 5 Problems
Async Script Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this async JavaScript execution in Selenium?
Consider the following Selenium Java code snippet that executes an asynchronous JavaScript script. What will be the value of
result after execution?Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver; Object result = js.executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "setTimeout(function() { callback('done'); }, 1000);" );
Attempts:
2 left
💡 Hint
Remember that executeAsyncScript waits for the callback to be called before returning.
✗ Incorrect
The async script uses setTimeout to call the callback with 'done' after 1 second. Selenium waits for this callback and returns its argument as the result.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies async script execution result?
You executed an async script that returns the string "success". Which assertion correctly verifies this in a JUnit test?
Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver; Object result = js.executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "callback('success');" );
Attempts:
2 left
💡 Hint
Use the assertion that compares values for equality.
✗ Incorrect
assertEquals compares the expected and actual values correctly. assertTrue with == compares references, which is unreliable for strings.
🔧 Debug
advanced2:00remaining
Why does this async script cause a TimeoutException?
This Selenium Java code throws a TimeoutException. Identify the cause.
Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver; Object result = js.executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "setTimeout(function() { console.log('done'); }, 5000);" );
Attempts:
2 left
💡 Hint
Check if the callback function is invoked in the script.
✗ Incorrect
executeAsyncScript waits for the callback to be called. Here, callback is never called, so Selenium waits until timeout and throws TimeoutException.
❓ framework
advanced2:00remaining
How to set async script timeout in Selenium WebDriver Java?
Which code snippet correctly sets the async script timeout to 5 seconds?
Attempts:
2 left
💡 Hint
Use the timeouts() method with setScriptTimeout and Duration.
✗ Incorrect
setScriptTimeout sets the timeout for async scripts. The other options set different timeouts or are invalid.
🧠 Conceptual
expert2:00remaining
What happens if async script callback is called multiple times?
In Selenium Java, if an async script calls the callback function more than once, what is the behavior?
Attempts:
2 left
💡 Hint
Think about how promises or callbacks usually behave in async APIs.
✗ Incorrect
Selenium treats the first callback call as the completion signal and ignores any further calls to the callback.