Monday, August 24, 2009

Selenium - Handling dialog box in MAC

What's the problem ?
How to Handle Dialog box in windows ?
How to handle Dialog box in MAC ?
Download the script ?
How to invoke the script/application. ?
Download the executable (app)

Selenium is a great tool to automate the web application. It supports you to test on multiple browser and on multiple Operating Systems. But it has few problems too. One if it's problem is it becomes blind when it comes to identify any of the dialog boxes. This is because dialog box are native components and selenium is designed to identify the web elements only (design bug, ahh just kidding)

How to Handle Dialog box in windows ?
You are bit lucky if you are using Windows Platform, coz then you can use Autoit to handle these dialog boxes. check this out the below link for
http://qtp-help.blogspot.com/2009/07/selenium-handle-dialogs.html

How to Handle Dialog box in MAC ??
But what if you are testing on mac? obviously you can't use AutoIt. But that doesn't mean you can't handle the dialog box on the mac. Mac provides better facility than windows in this case. You don't even need to download or install new software for this. Mac has the AppleScript which you can use to handle to dialog boxes.

Don't worry if you don't have much knowledge about AppleSctript, It's just like other scripting languages ( actually it's not, it will look bit weird if you have used Shell script or JavaScript before).

Before I found out that the applescript can solve the problem, and write down this piece of code I wasn't aware of the AppleScript, neither I have worked on the MAC in my life. So to write down this little piece of code took my whole 3 hours. But I am happy as it's working.

Following is the script to handle the Authentication dialog box of Firefox in mac.


(*
Created By: Gaurang Shah 
   gaurangnshah@gmail.com 
   
Purpose: To handle the Athentication dialog box in Firefox.
Usage: Filename UserName Password
*)

delay 10

on run argv
 tell application "Firefox"
  activate
 end tell
 
 
 tell application "System Events"
  if UI elements enabled then
   tell process "Firefox"
    set frontmost to true
   end tell
   
   if length of argv < 2 then
    display dialog "Usage: FileName UserName Password"
    return
   end if
   
   keystroke item 1 of argv
   keystroke tab
   keystroke item 2 of argv
   keystroke return
   
   
  else
   tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.universalaccess"
    display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
   end tell
  end if
 end tell
end run

Download the script

You can write down the similar script to handle other File Open and File Save Dialog boxes.

How to call the script
It's quite to easy to invoke the script.
Just open the script in Script Editor and save the script as application. In the Java File write down the following line just before the line that shows dialog box
dialog =  new String[]{ "Handle_Authentication_Dialog_FF.app","UserName","Password" };
//First String in the application name. Must be in your project folder. 
// Second is UserName to log in 
// Third is Password.
Runtime.getRuntime().exec(Dialog);

The other way is that you can open the Shell Prompt and pass the application name and it's arguments as follows. I am not sure this will work or not.
dialog =  new String[]{ "Shell prompt","Handle_Authentication_Dialog_FF.app","UserName","Password" };
//First String in the application name. Must be in your project folder. 
// Second is UserName to log in 
// Third is Password.
Runtime.getRuntime().exec(Dialog);

3 comments:

Unknown said...

Hi Gurang.. when I try to download your first script ie Following is the script to handle the Authentication dialog box of Firefox in mac. I get an error. I tried to copy paste and I see that there is a < tag near span thats missing. can you upload the correct file. Desperate need for this.

Unknown said...

Hi Yamini,

thanks for pointing out, I have corrected the code.

Suparna said...

Hi Gurang, When I call the script through selenium, it runs but I get the error :
Can’t get length. (-1728)

Post a Comment