04. Handling Javascript Alerts, Confirmations And Prompts

Alerts, Confirmation and Prompts are very commonly used elements of any software webpage and you must know how to handle all these popups In selenium webdriver software automation testing tool.

Alert Popup
Generally alert message popup display on page of software web application with alert text and Ok button as shown In bellow given Image.
  


Confirmation Popup Confirmation popup displays on page of software web application with confirmation text, Ok and Cancel button as shown In bellow given Image. 



Prompt Popup Prompts will have prompt text, Input text box, Ok and Cancel buttons.




Selenium webdriver software testing tool has Its own Alert Interface to handle all above different popups.
Alert Interface has different methods like accept(), dismiss(), getText(), sendKeys(java.lang.String keysToSend) and we can use all these methods to perform different actions on popups.

package Others;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class Alerts
{
            @Test
            public void Alets_Handling() throws InterruptedException
            {
                       
                        //Open the Chrome Browser
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        WebDriver driver = new ChromeDriver();

                        //Using Implicitly Wait Command
                        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                       
                        // Click the URL
                        driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
                       
                        // Observe in that page have "3 buttons there" 1. Show Me Confiramation 2. Show Me Alert 3. Show Me Prompt
                       
                        //Alert Pop up Handling - click the Show Me Alert Button
                        driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
                       
                        //To locate alert.
                        Alert A1 = driver.switchTo().alert();
                       
                        //To read the text from alert popup.
                        String Alert1 = A1.getText();
                        System.out.println(Alert1);
                        Thread.sleep(2000);
                       
                        //To accept/Click Ok on alert popup.
                        A1.accept();
                       
                        //Confirmation Pop up Handling - CLick the Show Me Confiramation Button
                        driver.findElement(By.xpath("//button[@onclick='myFunction()']")).click();
                        Alert A2 = driver.switchTo().alert();
                        String Alert2 = A2.getText();
                        System.out.println(Alert2);
                        Thread.sleep(2000);
                       
                        //To click On cancel button of confirmation box.
                        A2.dismiss();
                       
                        //Prompt Pop up Handling - Click the Show Me Prompt button
                        driver.findElement(By.xpath("//button[contains(.,'Show Me Prompt')]")).click();
                        Alert A3 = driver.switchTo().alert();
                        String Alert3 = A3.getText(); System.out.println(Alert3);
                       
                        //To type text In text box of prompt pop up.
                        A3.sendKeys("This Is John");
                        Thread.sleep(2000);
                        A3.accept();
            }
}


3 comments:

  1. Thanks for your blogs that are very helpful to learn the things .
    CREDO SYSTEMZ providing Best AngularJS Training in Chennai with Free Placement Assistance. You can start to Develop Powerful and Rich Web applications from the day 1 of our Best AngularJS Training.
    Angular JS Training in Chennai | Angular JS Training Institute in Chennai

    ReplyDelete