TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: 3 Unique Ways to Handle File Upload In Selenium
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

3 Unique Ways to Handle File Upload In Selenium

Last updated: Nov 05, 2023 6:19 pm
By Harsh S.
Share
8 Min Read
3 Unique Ways to Handle File Upload in Selenium Webdriver
3 Unique Ways to Handle File Upload.
SHARE

Today, we’ve come up with three test automation techniques that you can use to handle file upload in your Selenium Webdriver projects.

Contents
Prerequisites and Required ToolsFile Upload Form for Offline TestingFile Upload Techniques by TechBeamersSendKeys to Handle File UploadAutoIt Script to Handle File UploadJacob API to Handle File Upload

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.

3 Unique Ways to Handle File Upload in Selenium Webdriver
3 Unique Ways to Handle File Upload

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

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Advanced Selenium Tutorial

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
Loading
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Harsh S. Avatar
By Harsh S.
Follow:
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article JAVA INTERVIEW QUESTIONS EVERY SOFTWARE TESTER SHOULD KNOW Java Interview Questions for 2024
Next Article 3 Unique Ways to Generate Reports in Selenium Webdriver 3 Unique Ways to Generate Reports in Selenium

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x