Selenium WebDriver Case Studies & Practical Application 3 — Questions and Answers
Question 1: A team wants to run 200 Selenium tests in parallel to reduce CI time from 40 minutes to under 5 minutes. Which infrastructure choice best supports this goal?
- Run all tests sequentially on a single machine with a faster CPU
- Use Selenium Grid with multiple nodes or a cloud provider like Sauce Labs/BrowserStack (Correct answer)
- Reduce the test suite to fewer tests
- Use PhantomJS for headless parallel runs
Correct answer: Use Selenium Grid with multiple nodes or a cloud provider like Sauce Labs/BrowserStack
Selenium Grid distributes tests across multiple nodes simultaneously; cloud providers scale this further with on-demand browser slots.
Question 2: During a test of a data table with 1,000 rows, performance is poor because Selenium queries all rows repeatedly. What refactoring improves speed?
- Find all row elements once with findElements() and iterate the cached list (Correct answer)
- Use driver.navigate().refresh() between row checks
- Switch to implicit waits for table rows
- Use CSS selector instead of XPath only
Correct answer: Find all row elements once with findElements() and iterate the cached list
Calling findElements() once retrieves all matching elements in a single WebDriver call; iterating the cached Java list avoids repeated DOM queries.
Question 3: A test verifies an email is sent after form submission. The email service is slow (up to 30 seconds). What is the correct testing strategy?
- Poll the inbox using WebDriverWait with a 30-second timeout and a custom ExpectedCondition (Correct answer)
- Hard-code Thread.sleep(30000) after form submission
- Skip email verification in automated tests
- Use driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS)
Correct answer: Poll the inbox using WebDriverWait with a 30-second timeout and a custom ExpectedCondition
A custom ExpectedCondition inside WebDriverWait polls the inbox at intervals until the email arrives or the timeout expires, avoiding a fixed sleep.
Question 4: Your Selenium tests must validate that clicking a link opens a PDF in a new browser tab. What is the most practical assertion?
- Switch to the new tab with driver.switchTo().window(), get the URL, and assert it ends with .pdf (Correct answer)
- Use OCR to read the PDF content in the browser
- Assert driver.getTitle() equals 'PDF Document'
- Call driver.findElement() to locate PDF content elements
Correct answer: Switch to the new tab with driver.switchTo().window(), get the URL, and assert it ends with .pdf
Switching to the new tab and asserting the URL ends with .pdf (or contains a PDF MIME route) is reliable and does not require OCR.
Question 5: A Page Object for a search page grows to 800 lines with duplicate locator logic. Which refactoring best applies the Page Object Model principle?
- Split the page into component objects (SearchBar, ResultsList, Filters) and compose them in the page class (Correct answer)
- Move all locators to a constants file and keep one class
- Use inheritance so all pages extend a MegaSearchPage
- Replace all methods with a single generic interact() method
Correct answer: Split the page into component objects (SearchBar, ResultsList, Filters) and compose them in the page class
Decomposing a large page object into focused component objects follows the Single Responsibility Principle and improves maintainability.
Question 6: A test must drag a slider to set a value of 75 out of 100. The slider is 200px wide. Which Selenium approach is correct?
- Use Actions.dragAndDropBy() to move the slider thumb by the proportional pixel offset (Correct answer)
- Send the value '75' with sendKeys() on the slider element
- Use JavascriptExecutor to set the slider's value attribute directly
- Double-click the slider to enter edit mode
Correct answer: Use Actions.dragAndDropBy() to move the slider thumb by the proportional pixel offset
Actions.dragAndDropBy() moves the slider thumb by a calculated pixel offset, simulating real user drag behavior.
Question 7: After a production incident, the team discovers a Selenium test passed even though a critical button was hidden behind a cookie banner. What test design improvement prevents this?
- Add a setup step that dismisses the cookie banner before every test that requires button interaction (Correct answer)
- Increase the implicit wait timeout to allow the button to become visible eventually
- Use JavaScript click instead of WebDriver click to bypass visibility checks
- Reduce the screen resolution so the banner doesn't appear
Correct answer: Add a setup step that dismisses the cookie banner before every test that requires button interaction
Handling the cookie banner in a @Before setup method ensures the UI is in a known, clean state before each test interacts with page elements.
A team wants to run 200 Selenium tests in parallel to reduce CI time from 40 minutes to under 5 minutes.
Which infrastructure choice best supports this goal?