Selenium WebDriver Case Studies & Practical Application 2 β Questions and Answers
Question 1: A QA team needs to test a single-page application where the URL doesn't change on navigation. Which Selenium strategy best handles verifying page transitions?
- Check document.title via JavaScript executor after each action (Correct answer)
- Use driver.getCurrentUrl() after every click
- Call driver.navigate().back() to confirm history
- Assert on the HTTP response code via WebDriver
Correct answer: Check document.title via JavaScript executor after each action
In SPAs the URL rarely changes, so checking document.title (or a unique DOM element) via JavascriptExecutor is the most reliable signal of a page transition.
Question 2: After deploying a new build, automated tests start failing with StaleElementReferenceException on previously stable locators. What is the most likely root cause?
- The browser version changed
- The DOM is re-rendered after an AJAX call before the element is interacted with (Correct answer)
- The implicit wait was removed
- WebDriver session expired
Correct answer: The DOM is re-rendered after an AJAX call before the element is interacted with
StaleElementReferenceException typically means the DOM was updated (e.g., via AJAX re-render) after the element reference was captured, making it stale.
Question 3: A test suite runs fine locally but intermittently fails on CI with 'element not interactable'. The CI machine is slower. What is the best fix?
- Increase Thread.sleep() calls throughout the suite
- Replace implicit waits with explicit WebDriverWait for specific conditions (Correct answer)
- Downgrade the browser on CI to match local
- Switch to HtmlUnitDriver on CI
Correct answer: Replace implicit waits with explicit WebDriverWait for specific conditions
Explicit WebDriverWait polls for a specific condition (e.g., elementToBeClickable), making tests resilient to environment speed differences without arbitrary sleeps.
Question 4: Your team tests a file-upload feature that opens a native OS dialog. How should Selenium handle this scenario?
- Use Robot class to type the file path into the dialog
- Send the file path directly to the hidden input element using sendKeys() (Correct answer)
- Use AutoIT to control the OS dialog
- Both A and C are valid; B only works when the input is visible
Correct answer: Send the file path directly to the hidden input element using sendKeys()
Selenium can call sendKeys() with a local file path on an <input type='file'> element, bypassing the native dialog entirely.
Question 5: A login test must read credentials from environment variables to avoid storing secrets in code. Which approach is correct in a Java Selenium test?
- System.getenv("USERNAME") and System.getenv("PASSWORD") (Correct answer)
- driver.manage().getCookies().toString()
- Hardcode credentials inside a @BeforeClass method
- Use driver.executeScript("return process.env.PASSWORD")
Correct answer: System.getenv("USERNAME") and System.getenv("PASSWORD")
System.getenv() reads OS-level environment variables at runtime, keeping credentials out of source code.
Question 6: A checkout flow spans three pages. Tests frequently fail on page 2 because a spinner overlay blocks clicks. What is the recommended Selenium pattern?
- Add Thread.sleep(5000) before every action on page 2
- Use WebDriverWait to wait for the spinner element to become invisible before acting (Correct answer)
- Refresh the page until the spinner disappears
- Switch to a new window to bypass the spinner
Correct answer: Use WebDriverWait to wait for the spinner element to become invisible before acting
WebDriverWait with invisibilityOfElementLocated() pauses execution precisely until the spinner hides, then allows safe interaction.
Question 7: A Selenium Grid is set up with two nodes: one Windows/Chrome and one Linux/Firefox. A test must run on both. How should the test specify the target node?
- Set DesiredCapabilities (or Options) with the browser name and pass them to RemoteWebDriver (Correct answer)
- Use driver.switchTo().window() to choose the node
- Hardcode the node IP in the test class
- Call GridLauncher.main() inside the test
Correct answer: Set DesiredCapabilities (or Options) with the browser name and pass them to RemoteWebDriver
RemoteWebDriver accepts DesiredCapabilities or browser Options that the Grid hub uses to route the session to the matching node.
A QA team needs to test a single-page application where the URL doesn't change on navigation.
Which Selenium strategy best handles verifying page transitions?