Sunday 30 September 2012

Handling Alerts in Selenium/WebDriver

Hello All,

It has been a long long time, when i wrote something for you guys on my blog. Got some time to write something, so today it will be related to handling alerts in selenium/webdriver.This post is written keeping Selenium 2.0 in my mind. So it may not hold good for Selenium RC users.

Selenium can handle  java script alerts in a very effective manner but it can not handle modal alert box which gets created by OS itself e.g.:  java script error message alert. Now how do you handle these java script alerts, what can you do with those alerts, lets see one by one with the code.

Let us take a Scenario as an example -:
 Select a object from the page, Click on Delete button. It triggers a alert saying "Object is going to be deleted from the database, Click OK to Confirm or Cancel to Cancel the Operation".

We need to test the message of the Alert, Clicking Ok and Clicking Cancel also.
Steps to be Performed
=========================
Select the Object to delete from the page.
Click on Delete button, it triggers the Alert message.




/* Code to switch the control to Alert message*/
Alert testAlrt=driver.switchTo().alert();
testAlrt is the Controlling object of the Alert. Now you can use this object to Accept/get Text/Dismiss the alert.

/* To get the text from the alert */
String sAltText=testAlrt.getText();

/* To Accept any alert, it means Clicking OK button */
testAlrt.accept();

/* To dismiss any alert, It means Clicking Cancel button */
testAlrt.dismiss();

/* If the Alerts gets created using Ajax call, then you may need to wait till the Alert present */
WebDriverWait wdWait= new WebDriverWait(driver, 10);
wdWait.until(ExpectedConditions.alertIsPresent());



This is all about handling Alerts in your Selenium test scripts. Please give back a feedback about the post.

Thanks!!

3 comments:

  1. The key to happy users is to know there’s a problem before they do, and even better, fix it before they realise. How do you do this? You either sit and watch your system 24 hours a day, or you set up some Automation Testing monitoring. There’s lots of companies willing to take lots of money off your hands for very complex and fancy pieces of software that will do this, and there are lots of open-source solutions (some of them also very complex, and some of them very fancy) that will do the same.

    ReplyDelete
  2. Hi,

    There is a "forgot Password" link on login page. on clicking on this link, javascript pop will come "do you want to retrieve your password". Script is failing in 4/10 attepmts if i use below code:

    Alert testAlrt=driver.switchTo().alert();
    String sAltText=testAlrt.getText();
    testAlrt.accept();

    then i put the 10 secs explicit wait using

    WebDriverWait wdWait= new WebDriverWait(driver, 10);
    wdWait.until(ExpectedConditions.alertIsPresent());

    Still the the same behaviour and sometimes i get following exception:

    "org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for alert to be present"

    Also, i observed that the "forgot password" link in firefox instance launched by Selenium is not functional. However, in my firefox instance, the link is functional.

    Any idea? help is really appreciated.

    Thanks,
    Amit

    ReplyDelete
  3. Hi,Thanks for the post.Would have been great...if you had provided an example for the above concept given.

    ReplyDelete

Related Posts

Related Posts...