Welcome to my blog!!!
Have you all wondered how the file upload and download in selenium is automated ?
Here we are to take a look at it.
What is AutoIt?
Auto-It is a window scripting freeware tool to automate various tasks involving Windows and desktop applications.
Why we need AutoIt?
Web applications do not always confine themselves to working entirely on the web. sometimes they need to interact with the desktop to do things like downloads & uploads. Automating these sorts of workflow is tricky in Selenium. Selenium is confined to automating browsers, so desktop windows are out of scope. If you are looking to automate workflows that go from browser to desktop and back in selenium, then a little AutoIt is in order.
How to download and install AutoIT
To work with AutoIt we need finder tool and editor tool. Let's see how to install them and use it.
Step 1): Go to this link
Step 2): Download Finder Tool or otherwise known as element identifier by clicking on download AutoIt Button.
Step 3): To Download AutoIT Editor Tool go to the same link .
Step 4): Download AutoIT Editor tool by clicking on download Editor Tool.
Step 5): Two Setup Files "Au3Info_x64" and "SciTE" will be downloaded in the downloads folder.
Step 6): Double click and install both the exe file.
Step 7): After successfully installation – open up AutoIT Editor tool.
Go to ‘C:\Program Files (x86)\AutoIt3\SciTE’
Step 8): click on SciTE.exe file to open the editor tool. The Editor tool will be open as follows
Step 9): Now opens element Identifier (Finder Tool).
Go to ‘C:\Program Files (x86)\AutoIt3 ‘
Step 10): click on Au3Info_x64.exe file to install the finder tool. The Finder tool will be open as follows.
How to use AutoIT in Selenium Let us now see an example of how to test automate uploading a file in Selenium using AutoIT.
Step 1): Navigate to PracticeWebPagelink.
Step 2): Now open Finder Tool (Click on Au3Info_x64.exe) and Editor Tool (Click on SciTE.exe)
Step 3): Click on “choose file” button and uploader file window will be opened using selenium script.
Step 4): Now drag the finder Tool to the “file name” box element of the file uploader window to find the properties of that element. As shown in the following image.
Step 5): We can get the property value of the “file name” box element i.e. title=’Open’, class=’Edit’ and Instance=’1’. These values are then used in the AutoIT script which will be seen in the next step.
Step 6): Now, navigate to the Editor Tool Window and write a script to upload a file. These scripts are new to us, and there are additional methods. Currently, we can only focus on a few methods for uploading files using AutoIT. Similar to the Eclipse IDE, the Editor tool window displays the syntax as we type the script. As a result, AutoIT scripts are simple to write and understand.
The following are the methods which we will be using to upload a file in Editor Tool Script.
1.ControlFocus(“title”,”text”,”controlID’) -> Firstly we need to focus on the “Filename” box element. Under title argument we have the value as “Open” and in the Text argument it can be “left empty” and next is the controlID argument. In the ControlId arg, We actually combine the class and instance from the finder tool property (class + Instance). So here it will be “Edit1”.
Refer step 5 for the value.
Syntax:
AutoIt Script:
ControlFocus("Open"," ","Edit1")
2. ControlSetText(” title “,” text “,controlID ,” File path which need to upload” ) -> secondly, we need to send the filename path to that” filename” box element.
Syntax:
It has 4 arguments where Title will have “Open” as its value and the text arg can be “left empty” and the controlID arg will have” Edit1” value and in new Text argument we have to pass the “filename path”
AutoIt Script:
ControlSetText("Open", " ", "Edit1", "C:\Users\subas\OneDrive\Desktop\Picture\pic.jpg")
3. ControlClick(” title “,” text “,controlID ) -> Thirdly, we need to click the “open” button in the upload folder window.
For that we need to go to the Finder Tool and drag the finder tool to” open button” in the file uploader window to find the attribute or property information of that open button element.
Now the value of the attribute class is changed to “Button” and other attribute value title and instance are same
After getting the attribute value we can write the script in Editor tool.
Syntax:
AutoIt Script:
ControlClick("open", " ","Button1")
So, after writing the autoIt script in the editor tool it will look like this as follows,
Step 7): After writing the AutoIt Script for the file upload we must compile the script and create .exe file. For that first save the script in editor Tool with the file extension as "filename.AutoIt (au3)" and then we need to compile the AutoIt script by selecting Tools->compile the following window will be opened. Select exe as the output type and select output arch as 64 bit(depends on your window version mine is 64 bit) and then finally select compile script.
The exe file will be uploaded in the same folder.
Step 8): In Selenium script After the “choose file” click script is done we must use the .exe file to upload the file. The control immediately transferred to autoit .
Runtime.getRuntime().exec("C:\Users\subas\OneDrive\Desktop\Picture \fileupload.exe")
Example code:
After executing the script, the file will be uploaded from the desktop window to the application.
It was working for me. Hope it works for you too.
We have more methods in this AutoIt to explore. It is just the beginning.
Never Stop Learning!!!