Selenium WebDriver Technology & Digital Applications 4 — Questions and Answers
Question 1: Which Page Object Model (POM) design principle does Selenium automation best practice advocate?
- Separate test logic from UI interaction code by modeling each page as a class (Correct answer)
- Store all locators in a single global constants file
- Write all test steps inline inside test methods without abstraction
- Use only ID locators to ensure maximum stability
Correct answer: Separate test logic from UI interaction code by modeling each page as a class
POM advocates that each web page is represented as its own class containing its locators and interaction methods, keeping test scripts clean and maintainable.
Question 2: What is a StaleElementReferenceException in Selenium and when does it occur?
- It occurs when a previously located element is no longer attached to the DOM (Correct answer)
- It occurs when an element is hidden behind another element
- It occurs when the element's CSS style has changed
- It occurs when the WebDriver session has expired
Correct answer: It occurs when a previously located element is no longer attached to the DOM
StaleElementReferenceException is thrown when a WebElement reference becomes invalid because the page or the element has been refreshed or replaced in the DOM.
Question 3: Which WebDriver method returns the current page source HTML as a string?
- driver.getPageSource() (Correct answer)
- driver.getHtml()
- driver.getBodyContent()
- driver.getDocument()
Correct answer: driver.getPageSource()
driver.getPageSource() returns the complete HTML source of the currently loaded page as a String.
Question 4: When using Selenium Grid, what is the role of a 'node'?
- A machine that provides browser instances to execute tests (Correct answer)
- The central coordinator that receives test requests
- A proxy server that routes browser traffic
- A configuration file describing browser capabilities
Correct answer: A machine that provides browser instances to execute tests
In Selenium Grid, nodes are machines registered with the Hub that host one or more browsers and execute the actual test commands routed by the Hub.
Question 5: What does the ExpectedConditions.elementToBeClickable() condition verify in Selenium?
- That the element is visible and enabled so it can receive a click (Correct answer)
- That the element exists anywhere in the DOM
- That the element is at the top of the Z-index stack
- That the element's click event handler is registered
Correct answer: That the element is visible and enabled so it can receive a click
elementToBeClickable() waits until the element is both displayed and enabled, meaning it is ready to receive user interaction.
Question 6: What is the purpose of the @FindBy annotation in Selenium's PageFactory?
- To declare a WebElement field and its locator strategy for lazy initialization (Correct answer)
- To mark a method as a test step in POM classes
- To specify the expected URL for a page object
- To cache the element permanently after first lookup
Correct answer: To declare a WebElement field and its locator strategy for lazy initialization
@FindBy annotates WebElement fields in a Page Object class, and PageFactory.initElements() uses these annotations to lazily locate elements when they are first accessed.
Question 7: Which method would you use in Selenium to take a screenshot and save it to a file?
- ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) (Correct answer)
- driver.captureScreenshot('path')
- driver.saveScreenshot(new File('path'))
- ScreenshotUtil.capture(driver)
Correct answer: ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE)
Casting the driver to TakesScreenshot and calling getScreenshotAs(OutputType.FILE) returns a File object containing the screenshot that can then be saved or attached to a report.
Which Page Object Model (POM) design principle does Selenium automation best practice advocate?