Thursday 10 November 2016

How to handle popup dialogs

Hey

Now we will be seeing how we can handle popup Dialog.  In selenium there is build-in support for handling alerts.

JAVA code for handling alerts is

                                    Alert alert=driver.switchTo().alert();

With this object we can Accept,Dismiss or Read the content of the alert box.

                                     alert.accept();        // for accepting the alert box or clicking OK

                                     alert.dismiss();      // for dismissing or clicking CANCEL

                                     alert.getText();      // for getting the message displayed in Alert box


SCRIPT FOR HANDLING ALERT

import org.openqa.selenium.Alert;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class alertexample {

        public static void main(String[] args) {

public WebDriver driver;
        driver=new FirefoxDriver();
        driver.get("Any website");
        driver.findElement(By.name("btnLogin")).click();
WebDriverWait w=new WebDriverWait(driver,5);      //Wait for alert box to be displayed
w.until(ExpectedConditions.alertIsPresent());
Alert alert=driver.switchTo().alert();
alert.accept();
       driver.close();
}
}


0 comments: