Handling a simple JavaScript alert
Web developers use JavaScript alerts for informing users about validation errors, warnings, getting a response for an action, accepting an input value, and so on.
Tests will need to verify that the user is shown correct alerts while testing. It would also be required to handle alerts while performing an end-to-end workflow. The Selenium WebDriver provides an Alert
class for working with JavaScript alerts.
In this recipe, we will handle a simple alert box using Selenium WebDriver's Alert
class. A simple alert box is often used to notify the user with information such as errors, warnings, and success. When an alert box pops up, the user will have to click on the OK button to proceed, as shown in the following screenshot:
How to do it...
We will create a test a page on which, when a button is clicked, a simple alert box is displayed to the user. This test will also check that correct information is displayed in the alert box as follows:
@Test public void testSimpleAlert...