Automation Using Selenium Webdriver
Showing posts with label Download file using Selenium WebDriver with AutoIt and Sikuli. Show all posts
Showing posts with label Download file using Selenium WebDriver with AutoIt and Sikuli. Show all posts

Wednesday 19 October 2016

Download file using Selenium WebDriver with AutoIt and Sikuli

Download file using Selenium WebDriver with AutoItand 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 generatedownload_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}")

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();
  }
 }
}
AutoIT: Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
Once you install AutoIT in system. Write a autoit script to handle the authentication window.
WinWaitActive("Windows Security")
Send("username")
Send("{TAB}")
Send("password")
Send("{ENTER}")
Save this file as “auth.au3"
Right click on the file and chose “Compile Script (x86)” option and it will make “auth.exe”
Now write the sample java code to use it
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WinAuthentication_Autoit {

 public static void main(String[] args) {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.get("http://codesorcery.net/old/authentication/secret/");
  try {
   Runtime.getRuntime().exec("E:\\AutoIT\\Auth.exe");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}