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:
As mentioned above, clicking on the “Choose File” button would trigger the native OS file explorer dialog, as seen below:
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:
- An XPath of the input element: //input[@type = ‘file’].
- A local path to the file you want to upload.
Here is an example of how to use this action:
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:
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:
- Deep Link Toolkit Addon
- Siri Commands for iOS Addon
- Google Authenticator OTP Automation Addon
- Image Comparison Addon
- Explore many more Addons here!
Go ahead and give these Addons a try! Share your experience in the comments below 🙃