AutoIT for Desktop Automation

31 Aug 2021
7 min read

How to upload Files in Selenium Webdriver using AutoIT

What is AutoIT

AutoIT is a freeware scripting language designed for automating windows GUI and general scripting. It uses a combination of mouse movement, keystrokes and window control manipulation to automate a task that is not possible by a selenium web driver. In layman’s terms, AutoIT is just another automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather than Web Automation.  Just like Selenium IDE, it also gives you the recording capability which generates the scripts for you to use the same script in your test.

Why Use AutoIt? Selenium is an open-source tool that is designed to automate web-based applications on different browsers but to handle window GUI and non HTML popups in applications, AutoIT in Selenium is required as these window-based activities are not handled by Selenium.

Now, how to upload a file in selenium web driver using AutoIT. Here we need three tools in order to do this.

  • Selenium Webdriver
  • AutoIT editor and element identifier
  • The window that you want to automate

How to download and install AutoIT

Need to download and install the below software’s for AutoIT from the AutoIT Download link. 

  1. AutoIT Setup
  2. SciTE – AutoIT Editor

Step 1): For Installing AutoIT-Click on both AutoIT setups one by one.

Step 2): After successful installation – open AutoIT Editor.Go to ‘C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe’

and click on the ‘SciTE.exe’ file, the AutoIT editor opens as shown on the screen below.

Step 3) : Now opens element Identifier .Go to ‘C:\Program Files (x86)\AutoIt3\Au3Info_x64.exe’

And click on the ‘Au3Info_x64.exe’ file, the element identifier opens as shown in the screen below.

How to find an element using an element Identifier.
Using AutoIT Element Identifier, we can find elements in the file upload window. It finds the element of the window GUI and provides the attribute of the element like title, class, instance etc.
For Example, We have to upload a PDF file by clicking on the “Select PDF files” button and convert it to JPG. 
https://www.ilovepdf.com/pdf_to_jpg
After clicking on the ‘Select PDF files’ button, we need to call AutoIT Script. The control immediately moved to AutoIT after clicking on the ‘Select PDF files’ button. 
Step 1): Now open element Identifier- Go to ‘C:\Program Files (x86)\AutoIt3’ and click on ‘Au3Info_x64.exe’ file, the element identifier window opens as shown in below screen.

Step 2): Now open the file uploader window by clicking on ‘Select PDF files’ which is a windows activity.

Step 3): Drag the finder tool on the ” File name” box element of the file uploader window to find the basic attributes info as shown in the below screen with the arrow.

We can get the value of attributes i.e. Title=’Open’Class=’Edit’ and Instance=’1′ as shown below. These values are used in writing AutoIT scripts.

Step 4): Now open AutoIT script editor, goto ‘C:\Program Files (x86)\AutoIt3\SciTE’ and click on ‘SciTE.exe’ as shown in step 2.

Note: There are lots of methods available which we can use in a script according to the requirement, but right now we will focus on the below methods as these methods are required for writing file upload script. 
Below are the steps for writing file upload scripts. 

  1. ControlFocus(” title “,” text [Optional] “,controlID ) //Sets input focus to a given control on a window.
  2. ControlSetText(” title “,” text [Optional] “,controlID ,” File path which needs to upload ” ) // Sets text of a control.
  3. ControlClick(” title “,” text [Optional] “,controlID ) //Sends a mouse click command to a given control.

You can see a number of methods are displayed as shown in the screen below. The good feature of AutoIT is that it is somewhat like Eclipse that suggests you some of the methods.

Here in the AutoIT editor, we have selected the “ControlFocus” method. Now, we will take the values from element identifier for ‘ControlFocus’ and ‘ControlSetText’ methods as these methods works on the same element i.e. ‘File name’ text box.  For ‘ControlClick’ method need to capture values of different element i.e. ‘Open’ button.

Parameter values for ControlFocus method: This method sets focus to the ‘file name’ text box of the file uploader window.

  • The 1st parameter Title is ” Open “.
  • The 2nd parameter Text which is optional.
  • The 3rd parameter ControlID is the combination of class=’Edit’ and Instance=’1′ i.e., . ‘Edit1.’

Parameter values for ControlSetText method:   
This method is used to define the path of a file which we need to upload in the ‘file name’ text box. 

  • The 1st parameter Title is ” Open “.
  • The 2nd parameter Text which is optional.
  • The 3rd parameter ControlID is the combination of class=’Edit’ and Instance=’1′ i.e.,  ‘Edit1.’
  • The 4th parameter new text, we pass the path of the file which we need to upload.

Step 5): Now drag the finder tool on the “Open” button element of the file uploader window to find the basic attribute information.

Previous values ( i.e. attributes of ‘File name’ text box) overwrite with new values of the ‘Open’ button. You can see the class attribute is now changed to “Button” which was previously “Edit” in the AutoIT element identifier window.

We can get the value of attributes i.e. Title=’Open’Class=’Button’ and Instance=’1′ as shown below. 

Parameter values for ControlClick method: This method clicks on the ‘Open’ button of the file uploader window.

  • The 1st parameter Title is ” Open “.
  • The 2nd parameter Text which is optional.
  • The 3rd parameter ControlID is the combination of class=’Button’ and Instance=’1′ i.e., . ‘Button1.’

Step 6): You can see in the screen below that the AutoIT script is completed to handle file uploader. Now you can close the element identifier and save the script as ” FileUpload ” with .au3 extension at any location eg. (D:\AutoIT_1).

Before executing this script, you need to compile this script by following the below steps. 

  • Compile Script (x64) – If you have windows 64-bit machine, then write click and select Compile Script (x86)

OR

Compile Script (x86) – If you have windows 32-bit machine, then write click and select Compile Script (x86)

Step 7): ‘FileUpload exe’ file generated after compilation, you can see in the below screen. Now we can use this file in Selenium web driver script.

Step 8): Now we will use this AutoIT script in the Selenium web driver. 

Open Eclipse and start writing code.

When selenium clicks on the “Select PDF files’ button, the file uploader box opens.

Then we need to call AutoIT script, the control is immediately transferred to AutoIT in order to upload a file and then the control send back to selenium as shown below.

Step 9): Write selenium script in eclipse.

  • Runtime class allows the script to interface with the environment in which the script is running.
  • getRuntime() get the current runtime associated with this process.
  • exec() methods execute the AutoIT script ( FileUpload.exe ) .

The above line will call AutoIT script in selenium and upload the file.

Execute below Selenium script in Eclipse :

Output: ‘Access Card Form.pdf’ file uploaded successfully.

Open Source AutoIT Alternative Tools :

  • AutoHotkey
  • Sikuli
  • UI.Vision RPA
  • Automator
  • UiPath

Discover more from Tech Shaadi

Subscribe now to keep reading and get access to the full archive.

Continue reading