AutoIT in Selenium Tutorial: How to use it?

What is AutoIt in Selenium?

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 which is not possible by selenium webdriver.

How to download and install AutoIT

Step 1): Go to this link.

Step 2): Hover on ‘Autoit’ and ‘Autoit Editor’ dropdown.

Download And Install AutoIT

Step 3) Click on ‘AutoIT’ Downloads option.

Download And Install AutoIT

Step 4): Download “Autoit” by clicking on ‘Download Autoit’ button .

Download And Install AutoIT

Step 5): Now download “Autoit editor” by clicking on ‘Downloads’ button .

Download And Install AutoIT

Step 6) Click on the link as shown below.

Download And Install AutoIT

After download you will get two setup file as shown in below screen, first is AutoIt version 3 setup and second is Scitautoit3 .

Download And Install AutoIT

Step 7): For Installing AutoIT-Click on both AutoIT setup one by one .

Step 8): After successfully installation – open up AutoIT Editor.

Go to ‘C:\Program Files (x86)\AutoIt3\SciTE’

Download And Install AutoIT

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

Download And Install AutoIT

Step 9) : Now opens element Identifier .

Go to ‘C:\Program Files (x86)\AutoIt3 ‘

Download And Install AutoIT

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

Download And Install AutoIT

Note: Once you done with this element identifier you need to close manually, it will not close automatically.

How to use AutoIT in Selenium

Under this, we will see how to use AutoIt editor and how to find element on file uploader window through AutoIT Element Identifier (Element identifier is a tool like selenium IDE, identifier find the element of window GUI or non HTML popups and provide the attribute of element like title, class, instance ) and how to write script on AutoIT editor using 3 methods.

For Example: We will use “Write to us” page of guru99 to upload resume ( Doc file).

After clicking on ‘Choose File’ button from the “Write to us” page, we need to call AutoIT script. The control immediately transferred to autoit after clicking ‘Choose File’ by the below statement which takes care of uploading part.

Runtime.getRuntime().exec("E:\\AutoIT\\FileUpload.exe");

Finally, when we run selenium script-it will fill the form-> upload resume-> Submit form.

Use AutoIT In Selenium

Step 1): Now open element Identifier- Go to ‘C:\Program Files (x86)\AutoIt3’ and click on ‘Au3Info.exe’ file, the element identifier window opens as shown in below screen.

Use AutoIT In Selenium

Step 2): Now open file uploader window by clicking on ‘Choose File’ which is windows activity.

Use AutoIT In Selenium

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

Use AutoIT In Selenium

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 script example as explained in below step 4.

Use AutoIT In Selenium

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

Start writing a script for selecting a file to upload.

There are lots of method 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:

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

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

Use AutoIT In Selenium

Here in the AutoIT editor, we have selected “control focus” method. Element identifier is already opened and minimized as the element is already identified in above step 3. We can open it by maximizing it.

Now, we will take the values from element identifier for ‘ControlFocus’ and ‘ControlSetText’ methods as these methods works on same element i.e. ‘File name’ text box but 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.

  • 1st parameter title is ” Open “.
  • We ignore 2nd parameter, the text is not required.
  • 3rd parameter controlID is the combination of class=’Edit’ and Instance=’1′ i.e., . ‘Edit1.’
    ControlFocus("Open","","Edit1") // This method sets input focus to 'File name' text box.

Use AutoIT In Selenium

Parameter values for ControlSetText method :

This method is used to define the path of a file which we need to upload in ‘file name’ text box. In another way, we can say that this method is used to set the text to the input element.

  • 1st parameter title is ” Open “.
  • We ignore 2nd parameter, the text is not required.
  • 3rd parameter controlID is the combination of class=’Edit’ and Instance=’1′ i.e., ” Edit1 “.
  • 4th parameter new text, we pass the path of the file which we need to upload.
    ControlSetText("Open","","Edit1","E:\Resume\resume.doc") // This method input file path of a control.

Use AutoIT In Selenium

After following the above step, don’t close the windows (editor and element identifier), keep it remain open. You again need to open file uploader window as to find attributes of ‘Open’ Button as shown in below step 5.

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

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

Use AutoIT In Selenium

We can get the value of attributes i.e. title=’Open’, class=’Button’ and instance=’1′ as shown below. These values are used in writing Autoit script as explained in below.

Use AutoIT In Selenium

Parameter values for ControlClick method:

This method clicks on ‘Open’ button of the file uploader window.

  • 1st parameter title is ” Open “.
  • We ignore 2nd parameter; the text is not required.
  • 3rd parameter controlID is the combination of class and Instance, i.e., ” Button1 “.
ControlClick("Open","","Button1") //This method click on 'Open' button of file uploader.

Use AutoIT In Selenium

Step 6): You can see in below screen that AutoIT script is completed to handle file uploader.Now you can close the element identifier and save the script as ” FileUpload ” at the given location ( E:\AutoIT ).

Use AutoIT In Selenium

Now you can’t execute this script directly, you need to compile this script.

For compiling this script, you have two options ” compile script x64 ” and ” compile script x86 “, if you have windows 32-bit machine then u go with ” compile script x86 ” and for windows 64-bit machine then u go with ” compile script x64 .”

Use AutoIT In Selenium

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

Use AutoIT In Selenium

Now we will use this AutoIT script in Selenium web driver. Check below for output.

File Upload in Selenium using Autoit

In Selenium script, we find the elements of the form and fill the data in each element as required and upload ‘resume.doc’ file by executing AutoIT exe file generated from AutoIT script and then allow to submit the form in Selenium AutoIt script.

  • Open Eclipse and start writing code.
  • When selenium clicks on Choose File button, file uploader box opens.
  • Then we need to call AutoIT script, the control immediately transferred to AutoIT in order to upload a file and then control send back to selenium as shown below.

File Upload In Selenium Using Autoit

Step 1): Develop 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 ) .
Runtime.getRuntime().exec("E:\\AutoIT\\FileUpload.exe");

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

File Upload In Selenium Using Autoit

Step 2) : Execute the Selenium script in Eclipse.

importjava.io.IOException;		
import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.firefox.FirefoxDriver;		
public class FileUpload {				
public static void main(String[] args) throws IOException {								
    WebDriver driver=new FirefoxDriver();			
    driver.get("http://demo.guru99.com/test/autoit.html");			
    driver.findElement(By.id("postjob")).click();			

    driver.findElement(By.id("input_3")).sendKeys("Gaurav");                                 					
    driver.findElement(By.id("id_4")).sendKeys("test.test@gmail.com");					
    driver.findElement(By.id("input_4")).click();			
    // below line execute the AutoIT script .
     Runtime.getRuntime().exec("E:\\AutoIT\\FileUpload.exe");		
    driver.findElement(By.id("input_6")).sendKeys("AutoIT in Selenium");					
    driver.findElement(By.id("input_2")).click();
    driver.close();
     }
}

Step 3): Verify the output, resume.doc file uploaded successfully and thank you message will be displayed.

File Upload In Selenium Using Autoit

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 application. AutoIt in Selenium is required as these window based activity are not handled by Selenium.

Use AutoIt

Moving ahead in this AutoIt tutorial, we will learn how to upload a file in selenium web driver using AutoIt. Here we need three tools in order to this.

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

Conclusion

  • Downloaded and installed Element Identifier and AutoIT editor.
  • Opened the site on which to do the operation.
  • Element Identifier identifies the elements of file uploader window.
  • Prepared AutoIT script in the editor with the help of Element identifier.
  • Autoit script is used in selenium webdriver script.
  • Executed the selenium script.
  • Output: Successfully uploaded the file.