2016-05-08

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.

Let’s now look at the summary of the key techniques that we are going to present. We’ve covered both the native and hybrid methods that can support to handle file upload. We’ve given the hyperlinks; you can directly skip to the point of your interest. Though, we recommend you go through the whole post at least once to gain a better perspective.

Using SendKeys command.

Using AutoIt scripting.

Using Jacob com interface.

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.

How to create a Selenium Webdriver project in Eclipse?

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 do select the suitable option during installation. Also, make sure you just don’t copy the AutoIt files from another system, instead, let the installer run, as it not only copies all the required files but registers them with your system. It’s helpful in the application where you use AutoIt interfaces to work with the window based dialogs.

Important Note: We suggest to use the 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 issue 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> file. You’ll the 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.

3 Techniques to Handle File Upload In Selenium Webdriver.

File Upload Form.

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.

1- Use Selenium Webdriver SendKeys Command to Handle File Upload.

It’s the most basic technique to perform the upload of a file. Simply, 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.

2- Use AutoIt Script to Handle File Upload in Selenium Webdriver.

Firstly create a new file in your Eclipse project and name it as <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 file upload window. We’ll execute this file from Java code using the <Runtime.getRuntime().exec()> method.

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.

3- Use Jacob API to Handle File Upload Window.

It’s the most interesting 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.

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 knew a different approach to work with the file uploads, then please use the comment box or “contact us” form to share it. It’ll help us connect you more closely, and we’ll publish it to enlighten our readers.

All the Best,

TechBeamers.

The post 3 Techniques to Handle File Upload In Selenium Webdriver appeared first on Python, Java, TestNG, Selenium Webdriver Tutorials.

Show more