Tuesday, February 16, 2016

How to handle Java Scripts Alerts in Webdriver

In general JavaScript popups are generated by web application and can be handled by WebDriver.
Webdriver offers the ability to handle with javascript alerts using Alerts API
// Get a handle to the open alert, prompt or confirmation
 Alert alert = driver.switchTo().alert();
//Will Click on OK button.
alert.accept(); 
alert.authenticateUsing(Credentials credentials)


// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on the alert.
alert.getText(); 
//Will pass the text to the prompt popup

alert.sendkeys();
Alert is an interface. There below are the methods that are used
 Lets take an example 
1. Open google chrome 
2. Navigate to site : http://admin-demo.nopcommerce.com/login
3. Go to Catalog->Product tags
4. And delete the product

SCRIPT :