Bird
0
0

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?

hard📝 Application Q15 of 15
Selenium Java - JavaScriptExecutor
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();
");
AThis script will timeout immediately because callback is never called.
BThis script correctly polls for data and calls callback when ready.
CThis script blocks the browser and never calls callback.
DThis script should use executeScript instead of executeAsyncScript.
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes