Selenium WebDriver Case Studies & Practical Application 5 — Questions and Answers
Question 1: An e-commerce site applies discount codes via an API call triggered on blur of the coupon field. Tests verify the wrong final price. What is the fix?
- After typing the coupon, trigger blur explicitly with Actions or Tab key, then wait for the price element to update before asserting (Correct answer)
- Assert the price immediately after sendKeys()
- Reload the page after entering the coupon
- Disable the blur event in test mode
Correct answer: After typing the coupon, trigger blur explicitly with Actions or Tab key, then wait for the price element to update before asserting
Explicitly triggering blur (Tab key or Actions.click elsewhere) fires the API call; an explicit wait on the updated price element ensures the assertion is timely.
Question 2: A test suite must clean up test data created during each run to avoid polluting the database. Which approach is recommended when using Selenium with a live staging DB?
- Use @AfterEach/@After teardown to call an API or DB cleanup script that removes test records by a unique test-run ID (Correct answer)
- Truncate all tables after the suite
- Use driver.navigate().refresh() to reset state
- Rely on the next test run to overwrite old data
Correct answer: Use @AfterEach/@After teardown to call an API or DB cleanup script that removes test records by a unique test-run ID
Teardown hooks that delete records tagged with a unique run ID ensure each test is isolated without affecting other records.
Question 3: A Selenium test clicks a button that triggers an async REST call and updates a counter on screen. The test asserts before the counter updates. What is the correct fix?
- Use WebDriverWait to wait until the counter element's text changes to the expected value before asserting (Correct answer)
- Add a 2-second Thread.sleep() after the click
- Assert using JavaScript to read a DOM attribute instead
- Retry the click until the counter changes
Correct answer: Use WebDriverWait to wait until the counter element's text changes to the expected value before asserting
WebDriverWait with textToBePresentInElement polls until the counter displays the expected value, reliably synchronizing with the async REST response.
Question 4: A team manages 300 Selenium tests across 10 modules. They want to re-run only failed tests without restarting the full suite. Which mechanism supports this in TestNG?
- Use the testng-failed.xml generated after a run to re-execute only failed tests (Correct answer)
- Manually delete passing tests from the test class
- Set a global retry count of 3 in the TestNG suite XML
- Run tests with the --failed flag in Maven Surefire
Correct answer: Use the testng-failed.xml generated after a run to re-execute only failed tests
TestNG automatically generates testng-failed.xml after a run; executing this file reruns only the failed tests without modifying source files.
Question 5: A team integrates Selenium tests into a Jenkins pipeline. Tests should block a deployment when more than 5% fail. How is this threshold configured?
- Use the JUnit/TestNG results plugin in Jenkins to set an unstable/failure threshold on the test result percentage (Correct answer)
- Hard-code an exit code check in a bash script counting failed lines
- Send test results to Slack and manually approve the deploy
- Configure Selenium Grid to abort at 5% failures
Correct answer: Use the JUnit/TestNG results plugin in Jenkins to set an unstable/failure threshold on the test result percentage
Jenkins' JUnit plugin lets you configure unstable and failure thresholds based on the percentage of failed tests from the reported XML results.
Question 6: A Selenium test verifies a multi-step wizard. Step 3 fails inconsistently because step 2's 'Next' button is sometimes clicked before the form validation clears. What pattern resolves this?
- After clicking 'Next' on step 2, wait for the step 3 container element to be visible before interacting with it (Correct answer)
- Click 'Next' twice to ensure it registers
- Use JavascriptExecutor to skip directly to step 3
- Increase the implicit wait to 30 seconds for the entire suite
Correct answer: After clicking 'Next' on step 2, wait for the step 3 container element to be visible before interacting with it
Waiting for step 3's container to become visible confirms the wizard has fully transitioned before any step-3 interactions occur.
Question 7: A legacy application renders content inside nested iframes with no stable IDs. How should Selenium navigate to an element in the innermost iframe?
- Switch to each iframe in order using driver.switchTo().frame() with index or WebElement, then interact with the target element (Correct answer)
- Use findElement() with a deep XPath that crosses iframe boundaries
- Call driver.switchTo().defaultContent() to flatten all iframes
- Open the iframe URL directly in a new tab
Correct answer: Switch to each iframe in order using driver.switchTo().frame() with index or WebElement, then interact with the target element
WebDriver must switch into each iframe level sequentially; there is no cross-frame findElement—you must switchTo each frame in the nesting order.
An e-commerce site applies discount codes via an API call triggered on blur of the coupon field.
Tests verify the wrong final price.
What is the fix?