Saturday, 7 July 2012

Handling Windows Popups in Selenium

When trying to automate a web application, we get some scenarios where, we need to perform some action on the pop ups generated by windows.

These scenarios are -: 
1) File upload/Download
2) Closing Java Script Notification error dialog
3) Authentication Required box
4) Other unnecessary windows pop ups

As we all know, Selenium is a browser automation tool and can only work with HTML pages. So how to handle these above mentioned pop ups.

Solution is to use AutoIT to automate windows based pop ups and execute AutoIT scripts from Selenium scripts.

Let’s take an example of a problem. Sometimes we get a pop up asking for authentication in web page. It can halt your selenium scripts and needs a user intervention to close the pop up. We will use AutoIT scripts to solve this problem.

Perform below Steps :

1) Download AutoIt and install it in the local Machine.
2) create one file(say CloseAuthentication.au3) with .au3 extension in any location.
3) Write AutoIT code to close the window, Use window title for the same.

Code-: 

WinWaitActive("Authentication Required","","10")

If WinExists("Authentication Required") Then
 Send("userid{TAB}")
 Send("password{Enter}")
 EndIf

4) In different browser, Window title can be different, so you need to handle it in you Selenium code or in AutoIT script.
5) The above code is not the only code, you can enter user name and password and click on enter also. Once you start playing with the tool, you will get to know.
6) Now, we are ready to compile the program. Click on Tools -> Compile, it will create another file with extn .exe in the same location.
7) Call and execute the created .exe file from selenium scripts.

Code-:
Runtime.getRuntime().exec("C:\\AutoIT Script Location\\CloseAuthentication.exe");

The full path of the location of the autoIt .exe file will be provided to Runtime's exec method. This the java method to run any exe file from java code.

If you are using any other language binding for selenium scripts then you have to find out the way to execute the exe file in that language.

Thanks!!

No comments:

Post a Comment

Related Posts

Related Posts...