Friday 13 December 2013

Handle Popups and Alert messages from selenium rc


Pop-Upsare classified into three types.

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

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")

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]");
Global.selenium.chooseOkOnNextConfirmation();
Global.selenium.click("alert");
System.out.println("Clicked on OK button in alert box");
String alert=Global.selenium.getAlert();//This is needed as If an confirmation is generated but you do not consume it with getConfirmation, the next Selenium action will fail.
Global.selenium.type("lastname", "sravan");
Thread.sleep(10000); //just for to see actions by selenium. Not needed

Same way you can handle both prompt and confirmation boxes.

No comments:

Post a Comment