Selenium Testing Framework Selenium (WebDriver)Testing Framework 2 — Questions and Answers
Question 1: Which WebDriver method is used to switch control to a browser alert dialog?
- driver.switchTo().alert() (Correct answer)
- driver.getAlert()
- driver.alert().switchTo()
- driver.switchTo().popup()
Correct answer: driver.switchTo().alert()
driver.switchTo().alert() returns an Alert object that lets you accept, dismiss, or read the alert text.
Question 2: What does the Actions class method `moveToElement()` do in Selenium WebDriver?
- Clicks on the element
- Simulates hovering the mouse over the element (Correct answer)
- Drags the element to a new location
- Scrolls the page to the element
Correct answer: Simulates hovering the mouse over the element
moveToElement() moves the mouse pointer to the center of the specified element, simulating a hover action.
Question 3: Which implicit wait setting means WebDriver will poll for an element up to 10 seconds before throwing NoSuchElementException?
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)) (Correct answer)
- driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10))
- driver.manage().timeouts().setScriptTimeout(Duration.ofSeconds(10))
- driver.manage().timeouts().implicitlyWait(10)
Correct answer: driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10))
implicitlyWait(Duration.ofSeconds(10)) sets a global polling timeout for element lookup across all findElement calls.
Question 4: When using WebDriverWait with ExpectedConditions, what exception is thrown if the condition is not met within the timeout?
- NoSuchElementException
- TimeoutException (Correct answer)
- ElementNotInteractableException
- StaleElementReferenceException
Correct answer: TimeoutException
WebDriverWait throws a TimeoutException when the expected condition is not satisfied before the timeout elapses.
Question 5: Which CSS selector syntax correctly targets an input element with the placeholder attribute value 'Search'?
- input[placeholder='Search'] (Correct answer)
- input#placeholder='Search'
- input.placeholder{Search}
- input:placeholder('Search')
Correct answer: input[placeholder='Search']
Attribute selectors in CSS use brackets: element[attribute='value'] to match an element with a specific attribute value.
Question 6: What is the purpose of the `executeScript()` method in JavascriptExecutor?
- Runs external JavaScript files
- Executes JavaScript code synchronously in the browser context (Correct answer)
- Compiles JavaScript for testing
- Injects scripts into HTML source
Correct answer: Executes JavaScript code synchronously in the browser context
executeScript() runs synchronous JavaScript directly in the browser, returning any value produced by the script.
Question 7: Which WebDriver method retrieves the current URL of the browser?
- driver.getTitle()
- driver.getLocation()
- driver.getCurrentUrl() (Correct answer)
- driver.getUrl()
Correct answer: driver.getCurrentUrl()
driver.getCurrentUrl() returns the URL of the currently loaded page as a String.
Which WebDriver method is used to switch control to a browser alert dialog?