File Upload during Web Automation

     File upload is an everyday activity in daily life whenever users use applications to achieve their specific purposes. We are also uploading the file and testing the application features flow during manual and automation testing. Most of them are facing a challenge to upload files during the automation flow, in the past years I observed that the engineers are making such cases as non-automatable or partially automatable. In recent years, automation engineers find different ways to upload files during the automation flow.

They are achieving the file upload activity in three ways:

  • Using sendKeys method.
  • Using AutoIT tool.
  • Using Robot class.

     In this article, I would like to explain more about the Robot class method to upload files. We know that most of them will go first with the sendKeys method, why because it’s easy and more dependent on the object locator. In the SendKeys method, we place the path along with the file name in sendKeys so that the program is navigated to the mentioned path to fetch the file. To work with SendKeys, we need a proper input tag attribute type “file”.

     The second approach AutoIT tool is rarely used because we have to install the third-party software and also it will work with only Windows operating system; also we need to import separate libraries while using different predefined utility functions. The third approach is the help of the Robot class.

     We know that Robot class help in managing various activities such as performing some tasks, handling the keyboard functions, the mouse functions, and many more. Here we will understand certain methods that are helpful in controlling the keyboard and the mouse while an application is being tested using Selenium. We will discuss some methods here, KeyPress() is called when we want to press any key, KeyRelease() is called to release the pressed key, MouseMove() is called when moving the mouse pointer over ‘X’ and ‘Y’ coordinates, MousePress() is called when we want to press the left mouse button, MouseRelease() is called when to release the pressed mouse button.

     Robot class methods also help to handle the popups. Some challenges like to write different key events based on the operating system and also not being easy to switch between different windows. Anyway, I would like to share the file upload logic that was implemented to run on Windows and MAC operating systems. Following are the details:

     First, we have to identify the browse or file upload field object and click on that object, it will prompt the file upload window and it wait for the file path to be added. Using the below logic we can set the file path to the file upload window prompt,

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(<filePath>, null); 

Next, we have to declare Robot class object as below,

Robot robot = new Robot(); 

Following are the code snippet to do the key events to upload the file in the Windows systems,

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Following are the code snippet to do the key events to upload the file in the MAC systems,

robot.delay(2000);
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);

     I hope you really enjoyed reading this article and we have seen the file upload on the web page. We have also discussed various methods for handling file upload in Selenium (which includes methods such as using sendKeys, using AutoIT, and using Robot class). Also, covered the logic to upload the file using the Robot class on Windows and MAC systems. You can try to implement the logic in your automation scripts when you have such requirements.  

make it perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: