Interacting with Alerts using Selenium for Java
Interacting with Alerts using Selenium for Java
Learn how to interact with alerts using Selenium for Java
Hi, it’s Angie and in today’s recipe, I’ll show you how to handle alerts with Selenium WebDriver.
Let’s start with this basic alert that simply provides us with a message and an OK button. If we inspect this button, notice we see it here in the DOM. However, when we click on this, an alert appears, but that alert is not present in the DOM.
Let me show you how we access this in Selenium. But first, let’s grab this button’s ID.
How to Test Alerts with Selenium Java
Using our Selenium WebDriver driver object, we’re going to find that button element using findElement. We can do that by its ID. Then we’re just going to click it. And this will trigger the alert.
The Selenium driver has a switchTo method, which allows you to get an alert. And notice here it returns an alert object. So, we’ll set this equal to Alert.
Now, from here, we can access anything on that alert. For example, to get this text here, we can use this alert object, and say, alert.getText.
And to click this OK button, we can use this alert object again and call alert.accept.
And that’s it. We’ve clicked the button. We switched to the alert context. We’ve gotten the text from that alert window, and we pressed the OK button by calling accept.
Awesome.
How to Test Confirmations with Selenium Java
Now let’s look at a confirmation alert. If we click on this button, notice, now we get a message. But we also have two buttons, one that says Cancel, and one that says, OK.
Let’s go ahead and grab the ID for the trigger a confirmation button. And again, we’ll use Selenium to click that button. So, we pass in the ID, and we say, click.
We switchTo the alert and this time, let’s click the Cancel button. To do so, we can call this alert.dismiss method. However, if you wanted to click the OK button, that would be the accept method again.
How to Test Prompts with Selenium Java
Okay, one more, this prompt alert. This one contains a text area that allows you to enter data inside. It also has Cancel and OK buttons.
Let’s grab the ID here. We’ll find that button and click it.
We’ll switchTo().alert().
We’ll use the alert object to sendKeys. This is the method that we call in order to send text into that input field. I’ll say my favorite food is “nachos”.
Then finally we can click OK, or we can click Cancel. Again to click the Cancel button, we’ll call alert.dismiss. And to click the OK button, we’ll call alert.accept. Let’s say alert.accept here.
Summing It Up – Working with Alerts in Selenium Java
And there you have it. These are the recipes to deal with alerts. You call driver.switchTo, in order to get an alert object.
From there, you can use the getText method in order to read text from an alert.
You can call the accept method for any type of positive confirmation, so that would be OK buttons or Yes buttons, or any type of positive confirmation.
For negative confirmation, such as Cancel or No, then you would call the dismiss method.
And finally, in order to send text to an alert, you would call the sendKeys method.
Resources
- Git Repo – AlertsTests.java
- Applitools Kitchen – Alert Testing
Schedule a free Applitools Demonstration
Request DemoHere’s the code sample from our tutorial on how to interact with alerts using Selenium for Java