Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Showing posts with label How to handle alert.How to get text from alert box.. Show all posts
Showing posts with label How to handle alert.How to get text from alert box.. Show all posts

Saturday, 8 November 2014

What is an alert/How to handle alert in selenium.

Alert is a common scenario for all the testers.For example if you are performing an action on a specific button like delete or save then you will  get an alert box.
If you doing a registration on a website.Then you can get an alert box.This came for your confirmation.In this scenario I am going to tell you all when I clicked on delete button of my web application i get this alert box.Then i will tell How to handle this alert box.



Web Driver provides a more efficient manner to handle all these events through the Alert interface, using the following methods:
There are four methods to work with alertbox.
void dismiss();
void accept();
String getText();
void sendKeys(String keysToSend);

Case1:
How to click on cancel on alert Box?
If you want to click on cancel button then  use void.dismiss(); 
//click on delete button
driver.findElement(By.id('id of Delete button')).click(); 
Alert myAlert = driver.switchTo().alert(); //Switch to alert pop-up
myAlert.dismiss();//click on cancel button
Case2:
How to click on ok on alert Box?
If you want to click on ok button then  use void accept();
driver.findElement(By.id('id of Delete button')).click(); 
//click on delete button
Alert myAlert = driver.switchTo().alert(); //Switch to alert pop-up
myAlert.accept(); //click on ok button

Case3:
How to getText from alert Box?
If you want to getText from pop up.
//click on delete button
driver.findElement(By.id('id of Delete button')).click(); 
Alert myAlert= driver.switchTo().alert();//Switch to alert pop-up
System.out.println(myAlert.getText());

Case4:
How to enter keys in alert box and click on ok?
If you want to enter keys in alert box.
//click on delete button
driver.findElement(By.id('id of Delete button')).click(); 
Alert myAlert=driver.switchTo().alert();//Switch to alert pop-up
driver.switchTo().myAlert().sendKeys("Delete");//This will enter keys in Textbox
myAlert.accept();//click on ok button

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: