logo logo

How to Upload Files while Automating Web Tests (No Code Needed)

main post image

Uploading a file from your local machine to your web app is a pretty common scenario, that must be included in your web test automation sometimes to complete end to end testing flow. However, you might face some obstacles while attempting to upload files when you click on the upload button. It immediately opens the native OS file explorer dialog, where you are required to select the file that you want to upload. Selenium cannot automate the native OS file dialog, so you won’t be able to record your actions on that dialog.

Let’s have a look at this input element of type file:

input element of type file

As mentioned above, clicking on the “Choose File” button would trigger the native OS file explorer dialog, as seen below:

native OS file explorer dialog

This element can also be wrapped by another element with different styles, but as long as it’s an input element of type file, the File Uploader Addon (from TestProject’s Addon Library) will know how to handle it!

The File Uploader Addon

To overcome the limitation of uploading files and to simplify the whole process, we can use the File Uploader Addon.

This useful addon contains two 2 different methods (‘actions’ in TestProject terminology) for uploading a file in your web test:

1. Upload File using XPath

A non-element action (basically a test step that doesn’t require an element to run) that has 2 parameters:

  1. An XPath of the input element: //input[@type = ‘file’].
  2. A local path to the file you want to upload.

Here is an example of how to use this action:

Upload File using XPath

2. Upload File to Input Element

An element action that works directly on the input element. It works on two types of elements that are located by these XPaths: //button[text()=’Upload’] AND //input[@type = ‘file’].

This element action takes only one input parameter which is the local path to the file that you want to upload. Here is an example of how to use this action:

Upload file to input element

 

That’s it! We’ve managed to overcome the obstacle of file uploading to our web tests, and we did that without using a single line of code! 😲🎉

Want to explore and read about more Addons? Check out these tutorials we have lined up for you:

  1. Deep Link Toolkit Addon
  2. Siri Commands for iOS Addon
  3. Google Authenticator OTP Automation Addon
  4. Image Comparison Addon
  5. Explore many more Addons here!

Go ahead and give these Addons a try! Share your experience in the comments below 🙃

 

Amit Yahav

About the author

Amit Yahav

Backend developer & Test automation solution architect at TestProject.
Father of one, tech geek and Manchester United fan!

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

Selenium for Teams

Sharing and distributing Selenium tests has never been so easy! With TestProject's FREE Selenium based platform, you can finally create awesome tests with the freedom to collaborate with your team effortlessly.
Sign Up Now right arrow
FacebookLinkedInTwitterEmail