Selenium Testing Framework Case Studies & Practical Application 3 — Questions and Answers
Question 1: A Selenium test clicks a 'Delete Account' button but the confirmation dialog is a browser-native alert (not HTML). How should the test handle dismissing it?
- driver.findElement(By.id('confirm')).click()
- driver.switchTo().alert().accept() (Correct answer)
- Use Actions class to press Enter
- Use Robot class to click OK
Correct answer: driver.switchTo().alert().accept()
driver.switchTo().alert() switches WebDriver context to the native browser alert so it can be accepted or dismissed.
Question 2: A test automates a multi-step wizard where each step loads in the same URL but different DOM content. After clicking 'Next', the test immediately tries to interact with step 2 content and fails. The fix is:
- Add Thread.sleep(2000) after each Next click
- Wait for a step-2-specific element to be visible before interacting (Correct answer)
- Reload the page after each step
- Use driver.navigate().refresh()
Correct answer: Wait for a step-2-specific element to be visible before interacting
Waiting for a step-2-specific element ensures the DOM has updated before attempting interactions, avoiding the need for arbitrary sleeps.
Question 3: Your test suite runs against a staging environment but some tests depend on real third-party payment processing. The best practice is:
- Use the production payment gateway in staging tests
- Mock or stub the payment service and test the integration separately (Correct answer)
- Skip payment-related tests entirely
- Hard-code test credit card numbers directly in test code
Correct answer: Mock or stub the payment service and test the integration separately
Mocking the payment service isolates your UI tests from third-party dependencies and allows testing various payment scenarios reliably.
Question 4: A tester needs to verify that a tooltip appears when hovering over an icon. Which WebDriver approach achieves this?
- driver.findElement(icon).click()
- new Actions(driver).moveToElement(icon).perform() (Correct answer)
- driver.findElement(icon).sendKeys(Keys.TAB)
- JavascriptExecutor to trigger mouseover event
Correct answer: new Actions(driver).moveToElement(icon).perform()
Actions.moveToElement() simulates a genuine mouse hover, triggering CSS :hover states and JavaScript mouseover events that display tooltips.
Question 5: A team maintains 500 Selenium tests and wants to introduce test data management. Currently test data is hard-coded in each test. The best approach is:
- Use a Data Provider pattern reading from external CSV or JSON files (Correct answer)
- Keep data in test code but move it to constants files
- Use production database data directly in tests
- Generate random data with UUID in every test
Correct answer: Use a Data Provider pattern reading from external CSV or JSON files
Data Provider patterns with external files centralize test data, make it easy to add new scenarios, and separate test logic from test data.
Question 6: During automation of a file upload feature, driver.findElement(uploadInput).sendKeys(filePath) works but the team reports it fails in CI. The most likely cause is:
- sendKeys() doesn't support file uploads
- The file path is local and doesn't exist on the CI server (Correct answer)
- CI uses a different browser
- File uploads require special WebDriver permissions
Correct answer: The file path is local and doesn't exist on the CI server
The file path used in sendKeys() must be a valid path on the machine running the WebDriver session; CI servers have different file systems than local machines.
Question 7: A Selenium test needs to verify content inside an iframe embedded in the main page. Attempting to find elements inside the iframe fails. The fix is:
- Use a longer implicit wait
- Switch context with driver.switchTo().frame() before locating elements (Correct answer)
- Use JavaScript to query the iframe's document
- Find the iframe element and call .getText()
Correct answer: Switch context with driver.switchTo().frame() before locating elements
WebDriver must switch its context into the iframe using switchTo().frame() before it can locate or interact with elements inside it.
A Selenium test clicks a 'Delete Account' button but the confirmation dialog is a browser-native alert (not HTML).
How should the test handle dismissing it?