0
0
Selenium Javatesting~20 mins

Async script execution in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Script Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);"
);
A"undefined"
B"done"
Cnull
DTimeoutException
Attempts:
2 left
💡 Hint
Remember that executeAsyncScript waits for the callback to be called before returning.
assertion
intermediate
2: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');"
);
AassertEquals("success", result);
BassertTrue(result == "success");
CassertSame("success", result);
DassertNotNull(result);
Attempts:
2 left
💡 Hint
Use the assertion that compares values for equality.
🔧 Debug
advanced
2: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);"
);
AThe console.log causes the script to fail.
BThe setTimeout delay is too long and causes a timeout error.
CThe callback is never called, so Selenium times out waiting.
DexecuteAsyncScript does not support setTimeout.
Attempts:
2 left
💡 Hint
Check if the callback function is invoked in the script.
framework
advanced
2:00remaining
How to set async script timeout in Selenium WebDriver Java?
Which code snippet correctly sets the async script timeout to 5 seconds?
Adriver.setAsyncScriptTimeout(5);
Bdriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
Cdriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Ddriver.manage().timeouts().setScriptTimeout(Duration.ofSeconds(5));
Attempts:
2 left
💡 Hint
Use the timeouts() method with setScriptTimeout and Duration.
🧠 Conceptual
expert
2: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?
ASelenium returns the value from the first callback call and ignores subsequent calls.
BSelenium throws an exception on the second callback call.
CSelenium waits for all callback calls and returns the last value.
DSelenium concatenates all callback values and returns them.
Attempts:
2 left
💡 Hint
Think about how promises or callbacks usually behave in async APIs.