0
0
Selenium Javatesting~5 mins

Alert accept and dismiss in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an alert in Selenium WebDriver?
An alert is a popup box that appears on the webpage to show messages or ask for user confirmation. Selenium can switch to this alert to interact with it.
Click to reveal answer
beginner
How do you accept an alert in Selenium Java?
Use driver.switchTo().alert().accept(); to click the OK button on the alert popup.
Click to reveal answer
beginner
How do you dismiss an alert in Selenium Java?
Use driver.switchTo().alert().dismiss(); to click the Cancel button or close the alert popup.
Click to reveal answer
intermediate
What happens if you try to interact with an alert without switching to it first?
Selenium throws a NoAlertPresentException because it cannot find the alert unless you switch to it first.
Click to reveal answer
intermediate
Write the code snippet to accept an alert and verify the alert text is 'Are you sure?'.
String alertText = driver.switchTo().alert().getText(); assert alertText.equals("Are you sure?"); driver.switchTo().alert().accept();
Click to reveal answer
Which Selenium method is used to accept an alert popup?
Adriver.switchTo().alert().accept()
Bdriver.switchTo().alert().dismiss()
Cdriver.acceptAlert()
Ddriver.alert().accept()
What does driver.switchTo().alert().dismiss() do?
AClicks Cancel or closes the alert
BThrows an exception
CClicks OK on the alert
DSwitches to the alert without any action
Before accepting or dismissing an alert, what must you do?
ARefresh the page
BNo action needed
CClose the browser
DSwitch to the alert using driver.switchTo().alert()
What exception is thrown if you try to accept an alert when none is present?
ATimeoutException
BNoSuchElementException
CNoAlertPresentException
DElementNotVisibleException
How do you get the text displayed on an alert popup?
Adriver.getAlertText()
Bdriver.switchTo().alert().getText()
Cdriver.alertText()
Ddriver.getText()
Explain the steps to handle an alert popup in Selenium Java.
Think about how you interact with popups in real life.
You got /4 concepts.
    Write a simple code snippet to dismiss an alert and verify the alert text is 'Confirm delete?'.
    Use driver.switchTo().alert() methods.
    You got /4 concepts.