Selenium Testing Framework Technology & Digital Applications 4 — Questions and Answers
Question 1: In Selenium's Actions class, which method chain simulates a right-click (context-click) on an element?
- actions.contextClick(element).perform() (Correct answer)
- actions.rightClick(element).perform()
- actions.clickRight(element).perform()
- actions.click(element, Button.RIGHT).perform()
Correct answer: actions.contextClick(element).perform()
The contextClick() method of the Actions class simulates a right mouse button click and perform() executes the action.
Question 2: What is the correct way to handle a JavaScript alert dialog in Selenium?
- driver.switchTo().alert().accept() (Correct answer)
- driver.handleAlert(true)
- driver.dismissPopup()
- driver.getAlert().click()
Correct answer: driver.switchTo().alert().accept()
Switching to the alert with switchTo().alert() and then calling accept() clicks the OK button to confirm and dismiss it.
Question 3: Which HTTP method does Selenium WebDriver primarily use to communicate with the browser driver executable?
- REST over HTTP (JSON Wire Protocol / W3C WebDriver Protocol) (Correct answer)
- WebSockets
- gRPC
- SOAP over HTTP
Correct answer: REST over HTTP (JSON Wire Protocol / W3C WebDriver Protocol)
WebDriver communicates with browser drivers using a RESTful HTTP protocol — originally JSON Wire Protocol, now standardized as W3C WebDriver.
Question 4: What is the difference between driver.close() and driver.quit() in Selenium?
- close() closes only the current window; quit() closes all windows and ends the WebDriver session (Correct answer)
- close() ends the session; quit() only minimizes the browser
- They are identical in behavior
- quit() is for mobile; close() is for desktop browsers
Correct answer: close() closes only the current window; quit() closes all windows and ends the WebDriver session
close() disposes only the focused window while quit() terminates the entire browser process and releases the WebDriver session.
Question 5: In a Selenium test using TestNG, which annotation marks a method to run once before all test methods in a class?
- @BeforeClass (Correct answer)
- @BeforeTest
- @BeforeSuite
- @BeforeMethod
Correct answer: @BeforeClass
@BeforeClass runs once before the first test method in the current class, commonly used to initialize WebDriver.
Question 6: How can you capture a screenshot of the entire browser window in Selenium WebDriver?
- ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE) (Correct answer)
- driver.saveScreenshot('path.png')
- driver.captureEntirePage()
- WebDriver.screenshot(driver)
Correct answer: ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)
Casting the driver to TakesScreenshot and calling getScreenshotAs() returns the screenshot in the specified format (FILE, BASE64, or BYTES).
Question 7: Which locator strategy uses the visible text of a hyperlink to find it in Selenium?
- By.linkText() (Correct answer)
- By.anchorText()
- By.visibleText()
- By.href()
Correct answer: By.linkText()
By.linkText() locates an anchor element by its exact visible text, while By.partialLinkText() allows partial matching.
In Selenium's Actions class, which method chain simulates a right-click (context-click) on an element?