Selenium Testing Framework Selenium (WebDriver)Testing Framework 4 — Questions and Answers
Question 1: Which ChromeOptions method is used to run Chrome in headless mode in Selenium?
- options.setHeadless(true)
- options.addArguments("--headless") (Correct answer)
- options.enableHeadless()
- options.addExtension("headless")
Correct answer: options.addArguments("--headless")
Passing "--headless" via addArguments() starts Chrome without a visible UI, suitable for CI environments.
Question 2: What is the correct way to handle multiple browser windows in Selenium WebDriver?
- driver.getWindows() to iterate all windows
- driver.switchTo().window(windowHandle) after getting handles via driver.getWindowHandles() (Correct answer)
- driver.openWindow() and then driver.switchWindow()
- driver.manage().windows().switchTo(index)
Correct answer: driver.switchTo().window(windowHandle) after getting handles via driver.getWindowHandles()
driver.getWindowHandles() returns a Set of window handle strings; driver.switchTo().window(handle) switches context to the target window.
Question 3: Which ExpectedCondition checks that an element is present AND visible on the page?
- presenceOfElementLocated()
- visibilityOfElementLocated() (Correct answer)
- elementToBeClickable()
- stalenessOf()
Correct answer: visibilityOfElementLocated()
visibilityOfElementLocated() waits until the element is in the DOM and has non-zero dimensions, confirming it is visible.
Question 4: Which method clears the existing text from an input field before typing new text?
- element.clear() (Correct answer)
- element.reset()
- element.delete()
- element.empty()
Correct answer: element.clear()
element.clear() removes the current value from a text input or textarea before you send new keystrokes.
Question 5: What does a Page Object Model (POM) design pattern primarily aim to achieve in Selenium tests?
- Faster test execution
- Separation of UI element locators from test logic (Correct answer)
- Automatic screenshot capture on failure
- Parallel test distribution
Correct answer: Separation of UI element locators from test logic
POM encapsulates page elements and interactions in dedicated classes, decoupling locators from test code for maintainability.
Question 6: Which method in WebDriver is used to get all text content visible on a web element?
- element.getAttribute("value")
- element.getText() (Correct answer)
- element.getContent()
- element.getInnerText()
Correct answer: element.getText()
element.getText() returns the visible text of the element, excluding hidden elements and HTML tags.
Question 7: What is the role of `WebDriverManager` library in a Selenium project?
- It manages multiple WebDriver sessions in parallel
- It automatically downloads and configures the correct browser driver binary (Correct answer)
- It provides a remote WebDriver connection manager
- It manages test data for parameterized tests
Correct answer: It automatically downloads and configures the correct browser driver binary
WebDriverManager (by Boni García) resolves and downloads the matching chromedriver/geckodriver binary so you don't manage it manually.
Which ChromeOptions method is used to run Chrome in headless mode in Selenium?