Today, we’ve come up with three test automation techniques that you can use to handle file upload in your Selenium Webdriver projects.
We’ve practically verified all of these file upload methods, and you should be able to adapt them quickly. These are three unique ways that you can understand and integrate easily with your code.
In this post, we’ll bring AutoIt to your notice which is a Windows GUI automation tool, and share its potential to handle file upload workflows in a web application. You’ll also get to know about the Jacob COM interface and see how to use it with Selenium to interact with the file upload dialog.
Handle File Upload In Selenium – 3 Techniques
Before we step forward, it’s important to understand the prerequisites and list down the tools that you’ll need to apply the above file upload tricks. Please check out the necessary details below.
Prerequisites and Required Tools
1- You’ll either have to create a simple Selenium Webdriver project or add a TestNG project in Eclipse. Read any of the below posts if you want to know how to do it from scratch.
a. How to create a Selenium Webdriver project in Eclipse?
b. How to build your first TestNG test case in Eclipse?
2- Download the AutoIt software from here and install it on your system. If you are using a 64-bit system, then select the suitable option during installation. Also, make sure you don’t copy the AutoIt files from another system, instead, let the installer run. It not only installs all the required stuff but also registers them with your machine. It’s helpful in the application where you use AutoIt interfaces to work with the window-based dialogs.
Important Note: We suggest using tools that are consistent with the architecture of the OS you are using. Like, for 64-bit systems, install the JDK and Eclipse of the same flavor. If you follow this approach, you’ll eventually reduce the chances of any platform-specific issues while building your applications.
3- Download the AutoItX4Java jar file which is a Java API wrapper for the AutoIt functions. Use the Eclipse <Build Path->Configure> option to Add this file as an external library in your project.
4- Download the Jacob library package, it comes as a zip file. Unzip the <jacob-1.18.zip> (or <jacob-1.19.zip> whichever is available) file. You’ll find the following files under the <jacob-1.18> folder.
- jacob.jar.
- jacob-1.18-x64.dll.
- jacob-1.18-x86.dll.
Add the <jacob.jar> as an external jar into your Eclipse project while copy/paste the <jacob-1.18-x86.dll> to your project. It’ll get the DLL file added to it.
Now you are all set to look over the special techniques to handle file upload in Selenium Webdriver. Let’s start to review them one by one.
File Upload Form for Offline Testing
We’ll be using the following HTML code to test the file upload techniques. Please save it as <SeleniumWebdriverUploadFile.html> and copy/paste this file into the Eclipse project. So that we can readily determine the path of this file by using Java’s <user.dir> System property. We’ll load this file from the sample code provided under each technique.
<!DOCTYPE html> <html> <body> <form> Pick Any File to upload: <input type="file" name="uploadfile" id="uploadfile"> </form> </body> </html>
File Upload Techniques by TechBeamers
Handling file uploads is a complicated automation task. But you can manage to do it with ease using the below three methods.
SendKeys to Handle File Upload
It’s the most basic technique to perform the upload of a file.
Get the file upload element either by using the ID or Name. And call the Webdriver’s sendKeys() method to set the value of the file to upload. Check out the below code for clarity. You can add it as is to your project. Please make sure to replace the ID/Name accordingly.
public void UploadFileUsingSendKeys() throws InterruptedException { driver = new FirefoxDriver(); String workingDir = System.getProperty("user.dir"); String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; driver.get(filepath); WebElement fileInput = driver.findElement(By.name("uploadfile")); fileInput.sendKeys(filepath); // Added a wait to make you notice the difference. Thread.sleep(1000); driver.findElement(By.id("uploadfile")).sendKeys( "C:\\path\\to\\fileToUpload.txt"); // Added sleep to make you see the difference. Thread.sleep(1000); fileInput.sendKeys(filepath); }
AutoIt Script to Handle File Upload
Firstly create a new file in your Eclipse project and name it <File_upload_selenium_webdriver.au>. Add the following AutoIt script code into the newly created file. This script accepts a single argument which is the path of the file to upload. It’ll help us handle the file upload window. We’ll execute this file from Java code using the <Runtime.getRuntime().exec()> method.
WinWaitActive("File Upload") Send($CmdLine[1]) Send("{ENTER}")
Now, find out the Java code that opens the file upload window and runs the AutoIt script mentioned above to carry out the file upload.
public void UploadFileUsingAutoIt() throws InterruptedException, IOException { String workingDir = System.getProperty("user.dir"); String autoitscriptpath = workingDir + "\\" + "File_upload_selenium_webdriver.au"; driver = new FirefoxDriver(); String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; driver.get(filepath); // Added a wait to make you notice the difference. Thread.sleep(1000); driver.findElement(By.id("uploadfile")).click(); // Added sleep to make you see the difference. Thread.sleep(1000); Runtime.getRuntime().exec( "cmd.exe /c Start AutoIt3.exe " + autoitscriptpath + " \"" + filepath + "\""); }
Jacob API to Handle File Upload
It’s a native API technique that gives you full control to write Java code that can manage any GUI Window. But you need to make sure all the prerequisites and tools are in place before you get to run the below code.
public void UploadFileUsingJacobDll() throws InterruptedException { String workingDir = System.getProperty("user.dir"); final String jacobdllarch = System.getProperty("sun.arch.data.model") .contains("32") ? "jacob-1.18-x86.dll" : "jacob-1.18-x64.dll"; String jacobdllpath = workingDir + "\\" + jacobdllarch; File filejacob = new File(jacobdllpath); System.setProperty(LibraryLoader.JACOB_DLL_PATH, filejacob.getAbsolutePath()); AutoItX uploadWindow = new AutoItX(); driver = new FirefoxDriver(); String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; driver.get(filepath); Thread.sleep(1000); driver.findElement(By.id("uploadfile")).click(); Thread.sleep(1000); if (uploadWindow.winWaitActive("File Upload", "", 5)) { if (uploadWindow.winExists("File Upload")) { uploadWindow.sleep(100); uploadWindow.send(filepath); uploadWindow.controlClick("File Upload", "", "&Open"); } } }
We wish the above tricks would have given you a new perspective to handle file upload windows, and you’ll use them more efficiently in your Selenium projects.
If you know a different approach to work with the file uploads, then please let us know. It’ll help us connect you more closely, and we’ll publish it to enlighten our readers.
All the Best,
TechBeamers