How To Download File Using AutoIT In Selenium WebDriver
Download File Using AutoIT In Selenium WebDriver
Selenium can not handle file downloading because browsers use native dialogs for downloading files. Sometime we need to download file from AUT(Application Under Test). There are several ways to automate download file in Selenium but here we see download file using AutoIT in Selenium WebDriver.
Also Read: How To Upload File Using AutoIT in Selenium WebDriver
AutoIt Introduction:
AutoIt Tool is an open source tool. It is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
Now the question is how we do download file using AutoIT Tool in Selenium WebDriver.
Follow the below steps:
- Download Autoit tool from here and install it
- Open Programs – Autoit tool – SciTE Script Editor and add the below mentioned AutoIt script in Autoit editor and save it as ‘DownloadFile.au3’ in your system
- Convert it as ‘DownloadFile.exe’
- In Eclipse, add the below mentioned Selenium Script and run
Step 1: Download AutoIt tool and install
Step 2: Open SciTE Script editor and add the below mentioned AutoIt script and save it as ‘DownloadFile.au3’ in your system.
AutoIt Script:
; wait for 8 seconds to appear download and save dialog. Used class property of download dialog. WinWait("[CLASS:#MozillaDialogClass]","",8) ; Perform keyboard ALT key + s key to select Save File Radio button using keyboard shortcut. Send("!s") ; Wait for 9 seconds Sleep(9000) ; Press Keyboard ENTER button. Send("{ENTER}")
AutoIt Script Explanation:
Line 1 : WinWait(“[CLASS:#MozillaDialogClass]”,””,8)
Wait for 8 seconds to appear download and save dialog. Used class property of download dialog.
Line 2 : Send(“!s”)
Perform keyboard ALT key down + s + ALT key Up action to select Save File Radio button using keyboard shortcut.
Line 3 : Sleep(9000)
Wait for 9 seconds
Line 4: Send(“{ENTER}”)
After that it downloads the document
Step 3: Once the file is saved, we need to convert the ‘DownloadFile.au3’ to ‘DownloadFile.exe’. To do this we need to compile the ‘DownloadFile.au3’
Right click on the file ‘DownloadFile.au3’ and click on ‘Compile Script’ to generate an executable file ‘DownloadFile.exe’
Step 4: In Eclipse, add the below mentioned Selenium Script and run
Given clear explanation in the comments section with in the program itself. Please go through it to understand the flow.
In Eclipse
package softwareTestingMaterial; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FileDownloadAutoIt { public static void main (String [] args) throws Exception{ WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //To open URL driver.get("http://softwaretestingplace.blogspot.com/2015/10/sample-web-page-to-test.html"); //Download Text File driver.findElement(By.xpath("//*[@id='post-body-5864649494765988891']/div[1]/form/div[1]/a[1]")).click(); //To call the AutoIt script Runtime.getRuntime().exec("D:\\SoftwareTestingMaterial\\AutoIt\\DownloadFile.exe"); //'close' method is used to close the browser window //driver.close(); } }
In the above Selenium Script, we did call the AutoIt Script after clicking on the browser button which transfers windows popup box and download the required file.
Syntax:
Runtime.getRuntime().exec("File Path of AutoIt.exe");
Runtime.getRuntime().exec(“D:\\SoftwareTestingMaterial\\AutoIt\\Downloadfile.exe”);
This way we could download a file using AutoIT
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
hi your script is helped me thank you so much
Thanks
Thanks a lot , this helped me a lot .
Thanks Gaurav Kaushik