Upload File using Selenium & AutoIt

In this tutorial, we will use AutoIt tool with Selenium & Java to automate File Upload scenario.

AutoIt tool provides a Java library : AutoItX4Java.Jar to write a script in Java.

Pre Requisite:

  • Download AutoItX4Java.Jar: Click here
  • Download Jacob.JAR: Click here
  • Add above 2 Jars into build path of your Project.
  • Download and install AutoIt tool to inspect windows elements: Click here

Note: Once you download Jacob jar. You will get DLL file along with it. You need to add the path of this file into your script. Jacob DLL file comes in in x86 and x64 versions. Make sure that you use the correct DLL file.
JacobJar
Command to add DLL file Path


System.setProperty(LibraryLoader.JACOB_DLL_PATH, System.getProperty("user.dir") + "\\lib\\jacob-1.19-x64.dll");

We will use AutoIt tool to inspect elements of windows Popup as shown in below screenshot.
AutoItInspect

Following 2 Actions need to be Performed using AutoIt in order to upload a file:

  • Input file Path into text box
  • Click on Open button

In AutoIt, ControlId is one of the attribute used to identify the element and ControlId is a combination of Class + Instance.
If you see in the above screenshot, class and instance of a textbox are:
Class: Edit
Instance: 1
Hence, controlId for text box would be “Edit1
&
ControlId for open Button would be “Button1

Code to upload a file would be as follows:


//Create an object of AutoItX class
AutoItX autoItObj = new AutoItX();
// Find title of windows Popup using AutoIt inspect tool
String title = "Open";
String filePath = "D:\\TestImage.png";
//Wait for windows Popup to be visible. Maximum time out given is 20 seconds
Assert.assertTrue(autoItObj.winWaitActive(title, "Cancel", 20));
// Input File Path
autoItObj.controlSend(title, "", "Edit1", filePath);
// Click on Open button
autoItObj.controlClick(title, "", "Button1");

Complete script:

  • Launch Url
  • Click on Upload files button
  • Input File Path into windows Popup and submit it.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.jacob.com.LibraryLoader;
import autoitx4java.AutoItX;

public class AutoItTests {

	@Test
	public void testFileUpload() throws Exception {
		System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://fineuploader.com/demos.html");
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//*[text()='Upload files']/..")).click();

		System.setProperty(LibraryLoader.JACOB_DLL_PATH, System.getProperty("user.dir") + "\\lib\\jacob-1.19-x64.dll");
		AutoItX autoItObj = new AutoItX();
		String title = "Open";
		String filePath = "D:\\TestImage.png";
		Assert.assertTrue(autoItObj.winWaitActive(title, "Cancel", 20));
		autoItObj.controlSend(title, "", "Edit1", filePath);
		autoItObj.controlClick(title, "", "Button1");
	}

}

If you really like the information provided above, please don’t forget to hit a like on Facebook Page, you can also leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined offset: 0 in G:\PleskVhosts\automate-apps.com\httpdocs\wp-content\plugins\cardoza-facebook-like-box\cardoza_facebook_like_box.php on line 924