Upload File in Selenium using Jacob API

Profile picture for user arilio666

JACOB or java-com bridge is a superpower java has to call COM automation components from java. It uses the java native interface to make native calls to the COM libraries.

This article will show how we can upload files using JACOB in selenium. JACOB is used with autoitx4java to access autoitX through COM. Make sure AutoIt is installed before proceeding. It has autoitx packed, allowing AutoIt commands to function through COM object using JACOB.

Before that, we need to set up some things in our eclipse java project for this to work.

1. Download and set up the necessary jars and DLL files.

  • Download Jacob jar file from this link.
  • Download the AutoItX4Java jar from this link.
  • Copy Jacob jar and DLL files and paste them into the eclipse project into a new folder called 'lib.'
  • Create a folder called tools in your eclipse project and create a subfolder inside it called AutoIt.
  • Go to C:\Program Files (x86)\AutoIt3\AutoItX and copy/paste AutoItX3.dll inside /tools/autoit.
  • Add the Jacob and AutoIt jar from the lib folder to your project via build path -> add external jars.
  • Right click dll file from tools/autoit from the project and copy the path.
  • Open CMD in admin mode and type in the following command to register this dll.
regsvr32 C:\Users\arili\git\Assignments\Selenium\tools\autoit\AutoItX3.dll
  • This should register the DLL files to regedit.

2. Selenium

package week4.day2;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.jacob.com.LibraryLoader;
import autoitx4java.AutoItX;
import io.GitHub.bonigarcia.wdm.WebDriverManager;
public class JacobPartTwo {
public static void main(String[] args) throws Exception {
 String jacobDllVersionToUse;
 if (System.getProperty("sun.arch.data.model").contains("32")) {
  jacobDllVersionToUse = "jacob-1.18-x86.dll";
 } else {
  jacobDllVersionToUse = "jacob-1.18-x64.dll";
 }
 File file = new File("lib", jacobDllVersionToUse);
 System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
 WebDriverManager.chromedriver().setup();
 ChromeDriver driver = new ChromeDriver();
 driver.get("http://autopract.com/selenium/upload1/");
 driver.findElement(By.xpath("//span[@class=\"btn btn-success fileinput-button\"]")).click();
 Thread.sleep(2000);
 AutoItX x = new AutoItX();
 x.winWaitActive("Open");
 x.sleep(1000);
 x.send("C:\\Users\\arili\\git\\Assignments\\Selenium\\target\\H.png");
 x.sleep(1000);
 x.controlClick("Open", "", "&Open");
}
}
  • Now we are pointing out the DLL files, which are very important for connecting the bridge to the AutoIt functions inside java.
  • Then we visit the upload site and click on the upload button.
  • AutoItX object is created and imported.
 x.winWaitActive("Title of the upload dialogue box");
 x.sleep(1000);
 x.send("Path to the file that should get uploaded");
 x.sleep(1000);
 x.controlClick("Title of the upload dialogue box", "", "&Open");

We can see that my upload window dialogue box is 'Open' depending upon the name title changes. Doing all this will result in adequately uploading the file using JACOB.