Friday, May 16, 2014

File Downloading With AutoIt


Sometime we need to download file from AUT(Application Under Test) but there are several debates on it.We do not want to go for this but we learn how to download file from Firefox,IE and Chrome using AutoIt.There are several ways to automate download file form browser we learn later.

Using AuoIt have some Pros and Cons

Pros:
1. Make an exe file and call it from our code
2. Easy to use.

Cons:
1. It's main problem is platform dependent means it run on only windows. 

Now we go for the steps where we learn how to use AutoIt and configuration:

Steps:
1.Download AutoIt from here .Download both AutoIt Full Installation and AutoIt Script Editor.
2.Install both AutoIt Full Installation and AutoIt Script Editor in our desired directory as normal software installed.

3.Open the AutoIt Script Editor and write down this code for downloading file from IE(Tested on IE 6) and Firefox.

#include <file.au3>

_Log("----new session----")

if $CmdLine[0] <> 3 then
; Arguments are not enough
 msgbox(0,"Error","Supply all the Arguments, Browser name and path to upload")
_Log("Arguments are wrong")
Exit
EndIf

;Activate firefox/IE browser
If (StringCompare($CmdLine[1],"firefox",0) = 0) Then
    $waitTime=$CmdLine[3]
    if (WinWait("[Class:MozillaDialogClass]","",$waitTime)==0) Then
        _Log("Window Not Found From FireFox")
    Else
    _Log("Waiting For Download Window From FireFox")
    _FFDownload()
    EndIf

ElseIf (StringCompare($CmdLine[1],"ie",0) = 0) Then
    WinWait("File Download - Security Warning")
    _Log("Waiting For Download Window From IE")
    _IEDownload()
Else
    Exit
EndIf
;Used For IE and Tested on IE6
Func _IEDownload()

    $hResult = WinActivate("File Download - Security Warning")
    If($hResult == 0) Then
        _Log("Unable to find Download Window from IE")
    Else
        $IETitle=WinGetTitle("File Download - Security Warning")
        _Log("Download Window activated"&$IETitle)
        WinActivate($IETitle)
        ControlClick($IETitle, "","[CLASS:Button; INSTANCE:2]")
        _Log("FileChooser Window opend")
        _Log("CommandLine Parameter Found and Value is:"&$CmdLine[2])
        WinActivate("Save As")
        _Log("FileChooser Window opend"&WinGetTitle("Save As"))
        ControlSetText(WinGetTitle("Save As"),"","Edit1",$CmdLine[2])
        Send("!s")
    EndIf
EndFunc

;Used for FireFox Browser
Func _FFDownload()
    $hResult = WinActivate("[Class:MozillaDialogClass]");

    If($hResult == 0) Then
        _Log("Unable to find Download Window From FireFox")
    Else
        ; If firefox is set the save the file on some specif location without asking to user.
        ; It will be save after this point.
        ;If not A new Dialog will appear prompting for Path to save

        _Log("Download Window activated")
        ;To change the focus on save button
        Send("{TAB}")
        Sleep(400)
        Send("{TAB}")
        _Log("Change Focus to Save Button")
        ;This is use for if dialoguebox have save and openwith option
        Send("!s")
        ;Click on Enter to Save
        Sleep(400)
        Send("{ENTER}")
        _Log("Press Enter")
        Sleep(400)

        If(WinExists(WinGetTitle("[active]"))) Then
            WinActivate("Enter name of file to save to…")
            $title = WinGetTitle("[active]")
            if($title=="Enter name of file to save to…")Then
            _Log("Active Window Title Is:"&WinGetTitle("[active]") )
            _Log("EnterName Window Opened And Tiltle is:"&$title)
            ControlSetText($title,"","Edit1",$CmdLine[2])
            ; Save File
            _Log("CommandLine Parameter Found For FilePath and Value is:"&$CmdLine[2])
            Send("!s")
            EndIf
        EndIf

    EndIf
EndFunc
;used for logging
Func _Log($sLogMsg)
_FileWriteLog(@ScriptDir & "\AutoItLog.log",$sLogMsg)
EndFunc


4.Save the File from File->Save option.
  
5.Now we go to Tools->Build option from AutoIt Script Editor.After build this it creates a exe file with samename in the same directory where Script file saved.

6.Now we just call this from selenium Java code.

    WebDriver driver=new FirefoxDriver();
    driver.get("File Download URL");
    //driver.findElement(By.partialLinkText("click here")).click();

    
    //This the code to call exe and pass the command line argument
    Process pb=new ProcessBuilder("AutoIt Script exe Path","BrowserName","File download Location","10").start();


7. Using this AutoIt Script we can handle Open with and Save window or Save file using File chooser window cab be handled from firefox.This AutoIt script have also a log file writing mechanism it should be generated where exe file exists and name Should be AutoItLog.log .


5 comments:

  1. Q. Can this run on Grid configuration? [nikolmarku@gmail.com]

    ReplyDelete
    Replies
    1. I do not try this on grid but, if node machine is running on windows then it should be run because .exe file not on linux or mac machine though we can run exe in linux(ubuntu) using wine.
      We can use sikuli java api for this situation.

      Delete