Bird
Raised Fist0
Selenium Javatesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using executeAsyncScript in Selenium Java?
easy
A. To execute JavaScript code only once when the browser starts.
B. To execute synchronous JavaScript code instantly without waiting.
C. To pause the entire Selenium test execution for a fixed time.
D. To run JavaScript code that waits for asynchronous tasks to complete before returning.

Solution

  1. Step 1: Understand async JavaScript in Selenium

    Async scripts allow waiting for tasks like AJAX calls or timers to finish before continuing.
  2. Step 2: Role of executeAsyncScript

    This method runs JavaScript that signals completion via a callback, so Selenium waits properly.
  3. Final Answer:

    To run JavaScript code that waits for asynchronous tasks to complete before returning. -> Option D
  4. Quick Check:

    Async script waits for tasks = A [OK]
Hint: Async scripts wait for callback before continuing [OK]
Common Mistakes:
  • Confusing async with synchronous script execution
  • Thinking it pauses test for fixed time
  • Assuming it runs only once at browser start
2. Which of the following is the correct syntax to execute an async script in Selenium Java?
easy
A. driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);");
B. driver.executeAsyncScript("return 5 + 5;");
C. driver.executeScript("var callback = arguments[0]; callback(10);");
D. driver.executeAsyncScript(() -> { return 10; });

Solution

  1. Step 1: Identify async script syntax

    Async scripts require a callback function as the last argument to signal completion.
  2. 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.
  3. Final Answer:

    driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);"); -> Option A
  4. Quick Check:

    Callback with setTimeout = A [OK]
Hint: Async scripts need callback as last argument [OK]
Common Mistakes:
  • Not passing or calling the callback function
  • Using executeScript instead of executeAsyncScript
  • Wrong Java syntax for async scripts
3. What will be the output of the following Selenium Java code snippet?
Object result = driver.executeAsyncScript(
  "var callback = arguments[0]; setTimeout(() => callback('done'), 500);");
System.out.println(result);
medium
A. TimeoutException
B. null
C. done
D. undefined

Solution

  1. 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.
  2. Step 2: Check what is printed

    The returned value from executeAsyncScript is 'done', so System.out.println prints 'done'.
  3. Final Answer:

    done -> Option C
  4. Quick Check:

    Callback returns 'done' = C [OK]
Hint: Callback value is returned and printed [OK]
Common Mistakes:
  • Expecting null before callback runs
  • Confusing async script with synchronous
  • Assuming timeout error without delay
4. Identify the error in this Selenium Java async script usage:
driver.executeAsyncScript("setTimeout(() => { return 'done'; }, 1000);");
medium
A. Missing callback argument and not calling it to signal completion.
B. Using arrow function inside executeAsyncScript is not allowed.
C. Timeout value should be zero for async scripts.
D. executeAsyncScript cannot run setTimeout functions.

Solution

  1. Step 1: Check async script requirements

    Async scripts must accept a callback argument and call it to signal completion.
  2. 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.
  3. Final Answer:

    Missing callback argument and not calling it to signal completion. -> Option A
  4. Quick Check:

    Callback must be called = D [OK]
Hint: Always pass and call callback in async scripts [OK]
Common Mistakes:
  • Forgetting to pass callback as argument
  • Returning value instead of calling callback
  • Thinking arrow functions are disallowed
5. You want to test a web page that loads data dynamically after 3 seconds. Which async script approach correctly waits for the data load before continuing?
driver.executeAsyncScript(
  "var callback = arguments[0];
   var checkData = () => {
     if(document.querySelector('#data').innerText !== '') {
       callback('loaded');
     } else {
       setTimeout(checkData, 500);
     }
   };
   checkData();
");
hard
A. This script will timeout immediately because callback is never called.
B. This script correctly polls for data and calls callback when ready.
C. This script blocks the browser and never calls callback.
D. This script should use executeScript instead of executeAsyncScript.

Solution

  1. Step 1: Understand polling in async scripts

    The script repeatedly checks if '#data' element has text, calling callback only when data is loaded.
  2. Step 2: Verify callback usage and waiting

    Callback is called only after data is ready, so Selenium waits properly for dynamic content.
  3. Final Answer:

    This script correctly polls for data and calls callback when ready. -> Option B
  4. Quick Check:

    Polling with callback = B [OK]
Hint: Poll and call callback when ready to wait dynamically [OK]
Common Mistakes:
  • Not calling callback after data loads
  • Using executeScript which does not wait
  • Blocking browser with infinite loops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard