Selenium Testing Framework Case Studies & Practical Application 2 — Questions and Answers
Question 1: A QA team notices their Selenium tests randomly fail during CI runs but always pass locally. The root cause is most likely:
- Incorrect browser version on CI
- Race conditions due to missing explicit waits (Correct answer)
- Wrong WebDriver path configuration
- Tests running in headless mode
Correct answer: Race conditions due to missing explicit waits
Race conditions caused by missing explicit waits are the most common cause of intermittent Selenium failures in CI environments where timing differs from local machines.
Question 2: A team is automating an e-commerce checkout flow. After clicking 'Pay Now', a confirmation number appears in a modal. Which approach is best to capture this dynamic value?
- Thread.sleep(5000) then getText()
- WebDriverWait until element is visible, then getText() (Correct answer)
- Immediately call getText() after click()
- Refresh the page and search for the modal
Correct answer: WebDriverWait until element is visible, then getText()
WebDriverWait with an expected condition ensures the modal is visible before attempting to read its text, avoiding timing issues.
Question 3: Your Page Object Model class for a login page has grown to 400 lines with methods for admin login, user login, SSO login, and password reset. The best refactoring approach is:
- Keep it as one class for cohesion
- Split into separate page objects per login type (Correct answer)
- Move all methods to a base test class
- Convert all methods to static utilities
Correct answer: Split into separate page objects per login type
Splitting into focused page objects per login type follows the Single Responsibility Principle and makes maintenance easier.
Question 4: A Selenium test must validate a PDF report downloaded by clicking a button. What is the recommended approach?
- Use AutoIT to handle the download dialog
- Intercept the download URL and validate via HTTP request separately (Correct answer)
- Use Robot class to press Enter on the dialog
- Assert the download button is clickable as a proxy
Correct answer: Intercept the download URL and validate via HTTP request separately
Intercepting the download URL and validating the PDF content via an HTTP request is more reliable and faster than handling OS-level file dialogs.
Question 5: A team's test suite takes 45 minutes to run. They want to reduce it to under 15 minutes without removing tests. The best approach is:
- Run tests on a faster machine
- Parallelize tests using TestNG parallel execution with a Selenium Grid (Correct answer)
- Convert all tests to use headless Chrome
- Remove all Thread.sleep calls
Correct answer: Parallelize tests using TestNG parallel execution with a Selenium Grid
Parallel execution with Selenium Grid distributes tests across multiple nodes, dramatically reducing total wall-clock time.
Question 6: When automating a drag-and-drop feature in a modern React application, standard Actions.dragAndDrop() fails. The best workaround is:
- Use JavaScript to simulate mouse events via executeScript() (Correct answer)
- Skip testing drag-and-drop as it's untestable
- Use Robot class for mouse control
- Downgrade to an older browser version
Correct answer: Use JavaScript to simulate mouse events via executeScript()
Executing custom JavaScript that dispatches mousedown, mousemove, and mouseup events reliably simulates drag-and-drop when native WebDriver Actions fail on modern frameworks.
Question 7: A company migrates their web app from AngularJS to React. Their existing Selenium tests break because they relied on 'ng-' attributes for locators. The best fix is:
- Rewrite all tests from scratch
- Add test-specific data-testid attributes to React components and update locators (Correct answer)
- Use XPath with text() to find elements by label
- Switch to a different testing framework entirely
Correct answer: Add test-specific data-testid attributes to React components and update locators
Adding data-testid attributes creates stable, framework-agnostic locators that survive future technology migrations.
A QA team notices their Selenium tests randomly fail during CI runs but always pass locally.
The root cause is most likely: