Upload File In Selenium WebDriver Using AutoIt

As you know, File uploading Is very hard In selenium webdriver because It Is not able to handle file uploading dialog. So we will use AutoIT with selenium webdriver to upload file In web applications. We have already created AutoIt script (Script To Upload File.exe) In previous post which can select file from File Upload dialog. We will learn how to Integrate that AutoIt script with selenium webdriver In this section.

In java, It Is very easy to execute any executable file. We can run any executable script file using Runtime.getRuntime().exec("File Path") method. We will use this java method In our selenium webdriver test script to handle file upload dialog.

So now final selenium webdriver with AutoIt Integration test script Is as bellow.

Note : "Script To Upload File.exe" and "Test.txt" files 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.com/2014/01/textbox.html");
 }
 
 @Test
 public void testCaseOne_Test_One() throws IOException, InterruptedException {
  //Click on browse button.
  driver.findElement(By.name("img")).click();    
  //To execute autoIt script .exe file which Is located at E:\\AutoIT\\ location.
  Runtime.getRuntime().exec("E:\\AutoIT\\Script To Upload File.exe");
  }
}

Run above script In eclipse. 
  • It will click on browser button to open File Upload dialog using selenium webdriver.
  • Then Runtime.getRuntime().exec("E:\\AutoIT\\Script To Upload File.exe"); syntax will execute AutoIt generated "Script To Upload File.exe" file. It will select file and click on open button of File Upload dialog.
On completion of script, you will see that Test.txt file Is uploaded as shown In bellow given Image.


This way we can upload file using AutoIt In selenium webdriver.

Read next post to know how to download file using AutoIT + Selenium webdriver.

2 comments:

  1. Hi,
    I tried above code. But my script gets stuck as soon as following line is executed that opens browse window
    driver.findElement(By.name("img")).click();

    Plz suggest

    ReplyDelete
  2. Hi, Aravind
    I'm Mak and couldn't move forward now.
    Because the file "Script To Upload File.exe" not works, outside eclipse. So it couldn't work inside eclipse.
    would you be kind to change your HTML file to make your file "Script To Upload File.exe" work.

    thank very much.

    Mak

    ReplyDelete