Tuesday 2 December 2014

Download file using Selenium WebDriver with AutoIt and Sikuli

Selenium can not handle the Download file window. We can use third party tools to handle download file window. Some of the tools are AutoIT and Sikuli.

AutoIT: Save the below script with extension as download_file.au3 and compile it then it will generate download_file.exe file.
Local $pathToSave=$CmdLine[1]
Local $windHandle=WinGetHandle("[Class:IEFrame]", "")
Local $winTitle = "[HANDLE:" & $windHandle & "]";
;get coordinates of default HWND
Local $ctlText=ControlGetPos ($winTitle, "", "[Class:DirectUIHWND;INSTANCE:1]")

; wait till the notification bar is displayed
Local $color= PixelGetColor ($ctlText[0],$ctlText[1])
while $color <> 0
sleep(500)
$ctlText=ControlGetPos ($winTitle, "", "[Class:DirectUIHWND;INSTANCE:1]")
$color= PixelGetColor ($ctlText[0],$ctlText[1])

wend
; Select save as option
WinActivate ($winTitle, "")
Send("{F6}")
sleep(500)
Send("{TAB}")
sleep(500)
Send("{DOWN}")
sleep(500)
Send("a")

WinWait("Save As")
; activate Save As window
WinActivate("Save As")
; path to save the file is passed as command line arugment
ControlFocus("Save As","","[CLASS:Edit;INSTANCE:1]")
Send($pathToSave)
sleep(500)
;click on save button
ControlClick("Save As","","[TEXT:&Save]")

; wait till the download completes
Local $sAttribute = FileGetAttrib($pathToSave);
while $sAttribute = ""
sleep(500)
$sAttribute = FileGetAttrib($pathToSave)
wend
sleep(2000)
;close the notification bar
WinActivate ($winTitle, "")
Send("{F6}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{ENTER}")
Now write the sample java code to use it.
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class DownloadFile_IE_AutoIT {

 public static void main(String[] args) throws InterruptedException {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.manage().window().maximize();
  driver.get("http://www.seleniumhq.org/download/");
  driver.findElement(By.xpath("//*[@id='mainContent']/p[9]/a[2]")).sendKeys(Keys.ENTER);
  try {
   String path="E:\\AutoIT\\test232";
   String file="E:\\AutoIT\\download_file.exe"+" "+path;
         Runtime.getRuntime().exec(file);
     } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
Sikuli: Download Sikuli from https://launchpad.net/sikuli/+download
Once you install Sikuli in system. Write a below script to handle the download window in IE.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Key;
import org.sikuli.script.Screen;


public class DownloadFileIE_Sikuli {

 public static void main(String[] args) throws FindFailed {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  Screen screen = new Screen();  
  driver.manage().window().maximize();
  driver.get("http://www.seleniumhq.org/download/");
  driver.findElement(By.xpath("//*[@id='mainContent']/p[9]/a[2]")).sendKeys(Keys.ENTER);
  
  screen.rightClick("D:\\SaveAsIE1.png");
  screen.keyDown(Key.DOWN);
  screen.click("D:\\SaveAsIE2.png");
  
  screen.click("D:\\FileName.png");
  screen.type("a", Key.CTRL);
  screen.type(Key.BACKSPACE);
  screen.type("IEdriver");
  screen.click("D:\\SaveFile.png");
  System.out.println("File downloaded");
 }
}
Below are the images used in script:






3 comments:

  1. More than your post, I like the live traffic feed. Pretty selfish I'd say! :D

    ReplyDelete
  2. Hi Sunil
    the Autoit code is for which version of IE.

    ReplyDelete