MCQs on Handling Alerts Using Selenium Java
Which Selenium interface is used to handle JavaScript alerts?
a) AlertHandler
b) Alert
c) AlertManager
d) JavaScriptAlert
Answer: b) Alert
How do you switch the WebDriver's context to an alert?
a) driver.switchTo().alert()
b) driver.handle().alert()
c) driver.alert().switch()
d) driver.switchToAlert()
Answer: a) driver.switchTo().alert()
What method is used to accept an alert in Selenium?
a) alert.accept()
b) alert.ok()
c) alert.submit()
d) alert.confirm()
Answer: a) alert.accept()
Which method is used to dismiss an alert?
a) alert.reject()
b) alert.dismiss()
c) alert.cancel()
d) alert.ignore()
Answer: b) alert.dismiss()
To retrieve the text of an alert, which method is used?
a) alert.getText()
b) alert.getAlertText()
c) alert.read()
d) alert.alertText()
Answer: a) alert.getText()
What type of alert requires the user to enter text in an input box?
a) Simple Alert
b) Confirmation Alert
c) Prompt Alert
d) Text Alert
Answer: c) Prompt Alert
How do you enter text into a prompt alert using Selenium?
a) alert.type("text")
b) alert.sendKeys("text")
c) alert.input("text")
d) alert.setText("text")
Answer: b) alert.sendKeys("text")
What will happen if driver.switchTo().alert() is called when there is no alert present?
a) A NoAlertPresentException is thrown
b) The script continues without any exception
c) The method returns null
d) A TimeoutException is thrown
Answer: a) A NoAlertPresentException is thrown
How can you verify if an alert is present on the webpage?
a) Use driver.findElement(By.alert())
b) Use driver.switchTo().alert() and handle NoAlertPresentException
c) Use driver.isAlertPresent()
d) Use alert.isDisplayed()
Answer: b) Use driver.switchTo().alert() and handle NoAlertPresentException
Which WebDriver command would you use to perform both accept and dismiss actions on an alert depending on a condition?
a) alert.acceptOrDismiss(condition)
b) alert.toggle(condition)
c) if (condition) alert.accept(); else alert.dismiss();
d) alert.confirmOrCancel(condition)
Answer: c) if (condition) alert.accept(); else alert.dismiss();
To handle alerts automatically, which Selenium WebDriver method is most commonly used?
a) driver.switchTo().alert().accept()
b) driver.switchTo().alert().dismiss()
c) none
d) a and b
Answer: d)
When working with alerts, what is the best practice if your test script involves user input?
a) Use Thread.sleep() before switching to the alert
b) Use Explicit Waits to wait for the alert to appear
c) Use Implicit Waits for handling alerts
d) Handle alerts directly without any waits
Answer: b) Use Explicit Waits to wait for the alert to appear
How can you handle an alert if you need to perform an operation right after dismissing it?
a) alert.dismiss(); driver.findElement(By.id("element")).click();
b) alert.accept();
c) alert.dismiss();
d) alert.close(); driver.findElement(By.id("element")).click();
Answer: a) alert.dismiss(); driver.findElement(By.id("element")).click();
If an alert is not handled, what is the likely outcome in a Selenium test script?
a) The test script will continue without any issue
b) The test script will fail with an UnhandledAlertException
c) The test script will throw a NoSuchElementException
d) The test script will automatically accept the alert
Answer: b) The test script will fail with an UnhandledAlertException
Which alert handling method can be used to assert that an alert contains specific text?
a) assertEquals(alert.getText(), "expected text")
b) assertEquals(alert.getAlertText(), "expected text")
c) assertTrue(alert.contains("expected text"))
d) assertTrue(alert.getText().contains("expected text"))
Answer: d) assertTrue(alert.getText().contains("expected text"))
What exception is thrown when you attempt to interact with an alert that is not present?
a) NoSuchElementException
b) AlertNotPresentException
c) NoAlertPresentException
d) UnhandledAlertException
Answer: c) NoAlertPresentException
What Selenium method is used to automate clicking "Cancel" on a confirmation alert?
a) alert.cancel()
b) alert.dismiss()
c) alert.close()
d) alert.reject()
Answer: b) alert.dismiss()
No comments:
Post a Comment