Wednesday, August 31, 2011

Selenium1.0 - Handle Pop-Up and Alerts


Selenium1.0 - Handle Pop-Up and Alerts

As per my understanding Pop-Ups are classified into four types.

1. JavaScript alerts: By default Selenium suppress the alerts during test execution, so not visible on the browser. If the alerts are not handled properly Selenium through following exception "com.thoughtworks.selenium.SeleniumException: ERROR: There was an unexpected Confirmation! [Do you want to Continue?]". This alert is further classified into "ok" , "ok and cancel" alert. This alert can be handled in the following way.

assertEquals("Saved Sucessfully!", s.getAlert()); // After click

s.chooseOkOnNextConfirmation(); // Before Click

s.chooseCancelOnNextConfirmation(); // Before Click

s.getConfirmation(); // After click

For "ok and cancel" alert, you need to consume the alert with "s.getConfirmation();" other wise it will   display the exception saying "ERROR: There was an unexpected Confirmation!". Basically you should be using two statements: (ok/cancel)conformation before the click and consume after the click.

If you insert an alert statement when there is no alert on the web page, selenium will display following exception - com.thoughtworks.selenium.SeleniumException: ERROR: There were no alerts

2. HTML Pop-Ups using JQuery or other techniques : Here popup is constructed using div and is visible on the screen. This can be handled in the following way

s.click("popup_ok")

s.click("popup_cancel")

3. Windows Pop-Ups : These are windows based popup and is visible on the screen. Selenium is not capable to handle this kind of pop-up, need to use AutoItv3. To handle this pop-up record the script in AutoItv3, convert into exe format and then call from Java program.

Runtime.getRuntime().exec("C:\\OKbtn.exe ");


WinWaitActive("#32770", "Message from webpage",10)

WinFlash("Message from webpage","", 4, 500) ; Just to Flash the window

ControlClick("Message from webpage", "","[CLASS:Button; INSTANCE:1]");


For more information about AutoItV3 read this Post.

4. Model pop-ups


http://bharath-marrivada.blogspot.com/2010/12/selenium-simulate-modal-dialog-pop-up.html


---

4 comments:

  1. Bharath,

    I have an issue:
    In my application there is an alert on the Popup.
    So it is not recognized by selenium.

    Can you help me on this how to work with Alerts present on the popup.

    Thanks,
    Rama

    ReplyDelete
  2. Rama,
    Categorize your popup as stated above and apply solution accordingly.

    ReplyDelete
  3. Bharath,
    In my application if I click on addform button,a form will be opened as popup.
    After filling data in the form and click on save,an alert is coming.

    Selenium is not recognizing that alert.

    Help on this.

    Thanks,
    Rama

    ReplyDelete
  4. Hello Rama,

    Ask your application developers the kind of pop-up application is generating and apply the solution accordingly.

    Thoroughly read the complete post, before you proceed with any solution.

    ReplyDelete