Saturday, 1 February 2014

Handling the Pop-Ups and Java script alerts


·         Handling the Pop-Ups and Java script alerts
Some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:  

Ex :     


Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes. After you’ve triggered an action that opens a popup, you can access the alert with the following: 




This will return the currently open alert object. With this object you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts.
Example :
           //Alert Window Object
            Alert  alertWindow = driver.switchTo().alert();
           
            //Accepting the Alert
            alertWindow.accept();
           
            //Cancelling the alert
            alertWindow.dismiss();
           
            //Get the content of alert
   alertWindow.getText();

No comments:

Post a Comment