Automation Testing Selenium WebDriver 2 — Questions and Answers
Question 1: Which class in Selenium allows you to perform keyboard and mouse actions like drag-and-drop?
- Actions (Correct answer)
- Robot
- Keys
- Interactions
Correct answer: Actions
The Actions class in Selenium WebDriver provides complex user gesture support including drag-and-drop, hover, and key chords.
Question 2: How do you handle a JavaScript alert using Selenium WebDriver?
- driver.switchTo().alert().accept() (Correct answer)
- driver.alert().dismiss()
- driver.handleAlert()
- driver.switchTo().popup().accept()
Correct answer: driver.switchTo().alert().accept()
driver.switchTo().alert() returns the Alert interface, then accept() clicks OK and dismiss() clicks Cancel.
Question 3: What is Explicit Wait in Selenium?
- Waiting for a specific condition on a specific element before proceeding (Correct answer)
- A fixed Thread.sleep pause
- Waiting for the entire page to load
- A global wait applied to all elements
Correct answer: Waiting for a specific condition on a specific element before proceeding
Explicit Wait uses WebDriverWait and ExpectedConditions to wait for a specific condition on a targeted element before continuing.
Question 4: Which ExpectedCondition should you use to wait until an element is clickable?
- elementToBeClickable() (Correct answer)
- visibilityOf()
- presenceOfElement()
- elementIsEnabled()
Correct answer: elementToBeClickable()
ExpectedConditions.elementToBeClickable() waits until the element is visible and enabled so it can receive a click.
Question 5: How can you execute JavaScript directly within a Selenium test?
- Cast WebDriver to JavascriptExecutor and call executeScript() (Correct answer)
- Use driver.runScript()
- Use Actions.javascript()
- Use driver.execute()
Correct answer: Cast WebDriver to JavascriptExecutor and call executeScript()
By casting the WebDriver instance to JavascriptExecutor, you can call executeScript() or executeAsyncScript() to run JavaScript in the browser.
Question 6: What does the By.cssSelector() locator strategy use to find elements?
- CSS selector syntax matching element attributes and relationships (Correct answer)
- XPath expressions
- DOM node IDs only
- HTML tag names only
Correct answer: CSS selector syntax matching element attributes and relationships
By.cssSelector() leverages standard CSS selector syntax (class, ID, attribute, pseudo-selectors) to locate web elements.
Which class in Selenium allows you to perform keyboard and mouse actions like drag-and-drop?