Handling a confirm box alert
A confirm box is often used to verify or accept something from the user. When a confirm box pops up, the user will have to click either on the OK or the Cancel button to proceed, as shown in the following screenshot:
If the user clicks on the OK button, the box returns true
. If the user clicks on the Cancel button, the box returns false
.
In this recipe, we will handle a confirm box using the Selenium WebDriver's Alert
class.
How to do it...
Let's create a set of tests that handle a confirm box displayed on a page as follows:
In the
testConfirmAccept
test, we will accept the confirm box and verify a message on the page when the confirm box is accepted, that is, when the OK button is clicked as follows:@Test public void testConfirmAccept() { //Clicking button will show a Confirmation Alert with OK and Cancel Button WebElement button = driver.findElement(By.id("confirm")); button.click(); try { //Get the Alert Alert alert = driver...