My Thoughts: November 2013

Wednesday, November 27

Download a file using Selenium WebDriver with AutoIt Integration


Selenium can not handle file downloading because browsers use native dialogs for downloading files which cannot be controlled by JavaScript .

There are some third party tools using which we can automate download functionality.Some of the tools are AutoIt  and Sikuli.

I have used AutoIt for downloading file . If you want to use AutoIt script in your selenium script then
  1. Get an exe file of AutoIt script 
  2. Call the exe file from Selenium script 

1. Get .exe file of AutoIt script :

Here is the AutoIt Script for downloading a  file from website.This script takes one parameter as an input i.e the exact location from where we would like to download the file .And the  output (downloaded file) will be stored at C:\Users\Public\Downloads.

Save the below script with extension as Download.au3 and run it  then it will generate  Download.exe file

You can also download the .exe format of script from here .

#comments-start
InetGet ( "URL" ,"filename" , options , background)

>>URL URL of the file to download. The URL parameter should be in the form "http://www.somesite.com/path/file.html" - just like an address you would type into your web browser.

>>filename [optional] Local filename to download to. 

>>options [optional] 
0 = (default) Get the file from local cache if available.
1 = Forces a reload from the remote site.
2 = Ignore all SSL errors (with HTTPS connections).
4 = Use ASCII when transfering files with the FTP protocol (Can not be combined with flag 8).
8 = Use BINARY when transfering files with the FTP protocol (Can not be combined with flag 4). This is the default transfer mode if none are provided.
16 = By-pass forcing the connection online (See remarks). 

>>background [optional] 
0 = (default) Wait until the download is complete before continuing.
1 = return immediately and download in the background (see remarks). 

#comments-end


;Exact location of WebSite from where we would like to download the file.We are reading url from commandline
$URL =$CmdLineRaw 

;Local address to which we would like to download the file 
$filename = "C:\Users\Public\Downloads\Recognised_Student_Form.pdf"

InetGet ($URL, $filename , 1, 0)


2.Call the .exe file from Selenium script :

If you are using java then you can run the .exe file from your  selenium script using the below sample code:

Runtime.getRuntime().exec("Path of autoIt exe file");


Here is the Selenium code which will naviagte to the Oxford Application form .Then it will download  "Recognised Student 2013/14 - Application Form (230 kb)" file and save it at C:\Users\Public\Downloads with name as "Recognised_Student_Form.pdf".

import java.io.IOException;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FileDownload {
 
 WebDriver driver;
  
  @BeforeTest
   public void setUpDriver() {
    driver = new FirefoxDriver();
   }
  
  @Test
  public void start() throws IOException{
   driver.get("http://www.ox.ac.uk/admissions/postgraduate_courses/apply/application_forms.html");
   
   //Get the downloadable file location from the site with link name as "Recognised Student 2013/14 - Application Form (230 kb) "
   String href=driver.findElement(By.xpath(".//*[@id='aapplications_for_recognised_student_status']/div/ul/li[1]/a")).getAttribute("href");
   
   //Framing the command string which includes two parameters
   //Parameter 1-  Location where the Download.exe file is located
   //Parameter 2 - file location which we have stored in "href" variable 
   String command="Location where Download.exe file is saved"+" "+href;
   //If you happened to save Download.exe file  at "Public downloads" folder then command statement will be like 
      //String command="\"C:/Users/Public/Documents/Download.exe\""+" "+href;
   
      System.out.println("command is "+command);
      ArrayList argList = new ArrayList();
      argList.add(href);
      
      //Running the windows command from Java
      Runtime.getRuntime().exec(command);     
  }
  }
If you would like to verify the pdf content then check this article  ..!! :)