Selenium WebDriver : Handling Javascript Alerts, Confirmations And Prompts

Alerts, Confirmation in addition to Prompts are real unremarkably used elements of whatsoever software webpage in addition to you lot must know how to handle all these popups In selenium webdriver software automation testing tool. If you lot know, Selenium IDE has many commands to handle alerts, confirmations in addition to prompt popups similar assertAlertassertConfirmationstoreAlertverifyAlertPresent, etc.. First of all, Let me demonstrate you lot all 3 unlike popup types to take confusion from your hear in addition to thus nosotros volition come across how to handgrip them In selenium webdriver.

Alert Popup
Generally alarm message popup display on page of software spider web application amongst alarm text in addition to Ok push every bit shown In bellow given Image.



Confirmation Popup
Confirmation popup displays on page of software spider web application with confirmation text, Ok in addition to Cancel push every bit shown In bellow given Image.



Prompt Popup
Prompts volition stimulate got prompt text, Input text box, Ok in addition to Cancel buttons.



Selenium webdriver software testing tool has Its ain Alert Interface to handgrip all to a higher house unlike popups. Alert Interface has unlike methods like accept(), dismiss(), getText(), sendKeys(java.lang.String keysToSend) in addition to nosotros tin role all these methods to perform unlike actions on popups.


Look at the bellow given uncomplicated example of treatment alerts, confirmations in addition to prompts In selenium webdriver software automation testing tool. Bellow given example volition perform unlike actions (Like click on Ok button, Click on cancel button, recall alarm text, type text In prompt text box etc..)on all 3 form of popups using unlike methods of Alert Interface.

Run bellow given example In eclipse amongst testng in addition to detect the result.
package Testng_Pack;  import java.util.concurrent.TimeUnit;  import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace degree unexpected_alert {  WebDriver driver;   @BeforeTest  populace void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);   driver.get(" ");  }   @AfterTest  populace void tearDown() throws Exception {   driver.quit();  }   @Test  populace void Text() throws InterruptedException {   //Alert Pop upward Handling.   driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();   //To locate alert.   Alert A1 = driver.switchTo().alert();   //To read the text from alarm popup.   String Alert1 = A1.getText();   System.out.println(Alert1);   Thread.sleep(2000);   //To accept/Click Ok on alarm popup.   A1.accept();      //Confirmation Pop upward Handling.   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 push of confirmation box.   A2.dismiss();      //Prompt Pop upward Handling.   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 popular up.   A3.sendKeys("This Is John");   Thread.sleep(2000);   A3.accept();    } }

This agency you lot tin handgrip unlike form of alerts real easily using Alert Interface of selenium webdriver software automation testing tool. Next postal service volition demonstrate you lot how to handgrip unexpected alerts In selenium webdriver.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post