A confirm box is often used to verify or accept something from the user. When a confirm alert is displayed, the user will have to click on either the OK or the Cancel button to proceed, as shown in the following screenshot:
If the user clicks on the OK button, the confirm box returns a true
value response. If the user clicks on the Cancel button, then it returns false
.
A prompt box is often used to accept a value from a user. When a prompt box pops up, the user will have to enter a value and click on either 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 the input value. If the user clicks on the Cancel button, the box returns null
.
In this recipe, we will handle confirm and prompt boxes using the Selenium WebDriver's Alert
interface.
Let's create a set of tests that can handle a confirm box displayed on a page, as follows:
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 shown in following code:@Test public void testConfirmAccept() { // Click Confirm button to show Confirmation Alert box driver.findElement(By.id("confirm")).click(); // Get the Alert Alert alert = driver.switchTo().alert(); // Click OK button, by calling accept method alert.accept(); // Check Page displays correct message WebElement message = driver.findElement(By.id("demo")); assertEquals("You Accepted Alert!", message.getText()); }
testConfirmDismiss
test as shown in the following code, we will dismiss the confirm box by calling the dismiss()
method; this is the same as clicking the Cancel button:@Test public void testConfirmDismiss() { // Click Confirm button to show Confirmation Alert box driver.findElement(By.id("confirm")).click(); // Get the Alert Alert alert = driver.switchTo().alert(); // Click Cancel button, by calling dismiss method alert.dismiss(); // Check Page displays correct message WebElement message = driver.findElement(By.id("demo")); assertEquals("You Dismissed Alert!", message.getText()); }
Let's create a test that handles a prompt box. We will enter text into the prompt box's input field and later verify if the same value is displayed on the page, as shown in following code example:
@Test public void testPrompt() { // Click Confirm button to show Prompt Alert box driver.findElement(By.id("prompt")).click(); // Get the Alert Alert alert = driver.switchTo().alert(); // Enter some value on Prompt Alert box alert.sendKeys("Foo"); // Click OK button, by calling accept method alert.accept(); // Check Page displays message with value entered in Prompt WebElement message = driver.findElement(By.id("prompt_demo")); assertEquals("Hello Foo! How are you today?", message.getText()); }
Handling a confirm box works in a similar way to handling a simple alert box. To cancel a confirm box, the dismiss()
method of Alert
is used, as follows:
alert.dismiss();
To handle a prompt box, the Alert
interface provides an extra sendKeys()
method to enter text in the prompt box's input field. We can enter the text and either accept or dismiss a prompt box using the Alert
method, as follows:
alert.sendKeys("Foo"); alert.accept();
3.14.141.115