Selenium WebDriver Case Studies & Practical Application 4 β Questions and Answers
Question 1: A mobile-responsive website must be tested at viewport 375Γ812 (iPhone 14). How can a desktop Selenium test simulate this without a real device?
- Use ChromeOptions to set '--window-size=375,812' and add a mobile emulation device metrics override (Correct answer)
- Call driver.manage().window().maximize() then crop screenshots
- Use driver.switchTo().frame() to shrink the viewport
- Set DesiredCapabilities platform to 'iOS'
Correct answer: Use ChromeOptions to set '--window-size=375,812' and add a mobile emulation device metrics override
ChromeOptions mobile emulation overrides device metrics and user-agent, allowing Selenium to test responsive layouts on a desktop Chrome instance.
Question 2: A test automates a flight-booking site that uses dynamic IDs like 'btn_12849_submit'. Which locator strategy is most resilient to ID changes?
- CSS selector targeting a stable attribute like [data-testid='submit-booking'] (Correct answer)
- XPath using the full dynamic ID
- findElementById() with the dynamic ID
- findElementByLinkText() on the button label
Correct answer: CSS selector targeting a stable attribute like [data-testid='submit-booking']
Targeting a stable data attribute (data-testid) decouples tests from auto-generated IDs that change on every build.
Question 3: A Selenium test must verify that a dashboard chart renders correctly. Since chart content is drawn on a <canvas> element, what is the best approach?
- Take a screenshot and compare it pixel-by-pixel against a baseline image using a visual testing tool (Correct answer)
- Use findElement() to locate chart bars inside the canvas
- Read the canvas innerHTML to verify data
- Call executeScript('return canvas.data') to get chart values
Correct answer: Take a screenshot and compare it pixel-by-pixel against a baseline image using a visual testing tool
Canvas content is not in the DOM, so visual (screenshot diff) testing tools like Applitools or Percy are the standard approach for canvas validation.
Question 4: Your CI pipeline must generate a test report that non-technical stakeholders can read. Which approach fits best with a Selenium/Java project?
- Integrate Extent Reports or Allure to generate HTML reports with screenshots on failure (Correct answer)
- Print test results with System.out.println() to the build log
- Export raw JUnit XML to stakeholders
- Use driver.getPageSource() as the report body
Correct answer: Integrate Extent Reports or Allure to generate HTML reports with screenshots on failure
Extent Reports and Allure produce rich HTML reports with logs and embedded screenshots that are readable without technical background.
Question 5: A test scenario requires logging in as three different user roles (Admin, Editor, Viewer) to verify permissions. What is the most efficient Selenium approach?
- Parameterize the test with a data provider, passing each role's credentials as a separate test run (Correct answer)
- Write three identical test methods, one per role
- Create three separate browser sessions in the same test method sequentially
- Hard-code the Admin user and add if/else branches for the other roles
Correct answer: Parameterize the test with a data provider, passing each role's credentials as a separate test run
Data-driven parameterization (TestNG @DataProvider or JUnit @ParameterizedTest) runs the same test logic against multiple credential sets without code duplication.
Question 6: A regression suite has 500 tests but only 50 cover the checkout flow changed in the latest sprint. How should the team optimize CI run time?
- Tag checkout tests and run only that tagged subset in the sprint's CI pipeline, running the full suite nightly (Correct answer)
- Delete all non-checkout tests until the sprint ends
- Run all 500 tests but skip assertions for non-checkout pages
- Increase thread count to 500 to run all tests simultaneously
Correct answer: Tag checkout tests and run only that tagged subset in the sprint's CI pipeline, running the full suite nightly
Tagging and running only affected tests in the sprint pipeline speeds up feedback; the full suite still runs nightly for regression coverage.
Question 7: A test must verify a tooltip appears when hovering over an icon. Which Selenium action achieves this?
- new Actions(driver).moveToElement(icon).perform() (Correct answer)
- driver.findElement(icon).click()
- new Actions(driver).doubleClick(icon).perform()
- driver.findElement(icon).sendKeys(Keys.TAB)
Correct answer: new Actions(driver).moveToElement(icon).perform()
Actions.moveToElement() moves the mouse over the element, triggering CSS/JS hover states that display the tooltip.
A mobile-responsive website must be tested at viewport 375Γ812 (iPhone 14).
How can a desktop Selenium test simulate this without a real device?