Search

Sunday, December 28, 2014

Downloading File Using AutoIt In Selenium WebDriver

Downloading file using selenium webdriver Is also tricky task. I have already described how to handle file download functionality In selenium webdriver by creating custom firefox profile In THIS POST. It Is really very long task. At place of using custom profile approach, We can use AutoIt script with selenium webdriver to download file from any web page.

You will find different test files download links on THIS PAGE which we will use to learn how to download file using AutoIt In selenium webdriver. We have to perform bellow given steps to create selenium webdriver + AutoIt script.

Identify objects properties of file download and save dialog
In Firefox browser, Go to THIS PAGE and click on "Download Text File" link. It will open Save file dialog with Open with and Save File radio options.

Now you can Identify save file dialog properties using AutoIt Window Info tool as described In THIS POST. In this case you will get only dialog's property (Title and class) using AutoIt Window Info tool as shown In bellow Image. It Is not able to retrieve property of Save File radio option and OK button. So we have to use some alternative method In our AutoIt script to select Save File radio option and click OK button.


So we have only dialog property In this case as bellow.
  • File Save dialog : Title = Opening Testing Text.txt, Class = MozillaDialogClass
We will use class property In our script to select dialog.

Write AutoIt script to save file
If you see In above Image, Save File option has shortcut to select It. We can select It using ALT + S keyboard shortcut keys. Also focus Is set by default on OK button. So we can press ENTER key to select OK. We can do both these Keystroke action In AutoIt very easily using Send() command as shown In bellow 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 down + s + ALT key Up action to select Save File Radio button using keyboard sortcut.
Send("{ALTDOWN}s{ALTUP}")

; Wait for 3 seconds
Sleep(3000)

; Press Keyboard ENTER button.
Send("{ENTER}")

Save above script In "Script To Download File.au3" format at "E:\AutoIT" location. Above script will perform bellow give actions.
  1. wait for 8 seconds to appear file saving dialog.
  2. Press ALT down + s + ALT up key to select Save File radio.
  3. Press Enter key to select OK button.
Convert script In executable format

Now you can create executable file of above script as described In THIS POST. After conversion, you will get executable file "Script To Download File.exe".

CLICK HERE to download ready made "Script To Download File.au3" and "Script To Download File.exe" files of AutoIt.

Integrate AutoIt script with selenium webdriver to download file
As described In THIS POST, We will use java Runtime.getRuntime().exec(); method to execute AutoIt script In selenium webdriver.

So our Selenium webdriver + AutoIt script to download file from web page Is as bellow.

Note : "Script To Download File.exe" file should be located at E:\\AutoIT folder.
package AutoIt;

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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AutoIt_Test {
WebDriver driver;

@BeforeTest
public void setup() throws Exception {
driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
}

@Test
public void testCaseOne_Test_One() throws IOException, InterruptedException {
//Click on Download Text File link to download file.
driver.findElement(By.xpath("//a[contains(.,'Download Text File')]")).click();
//Execute Script To Download File.exe file to run AutoIt script. File location = E:\\AutoIT\\
Runtime.getRuntime().exec("E:\\AutoIT\\Script To Download File.exe");
}
}

Now If you run above script, It will download Text file automatically. At the end of script execution, file will be downloaded as shown In bellow Image.


This way you can download any file from web page.

No comments:

Post a Comment