[Selenium+Java] How to use AutoIT with Selenium

Posted alicegu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Selenium+Java] How to use AutoIT with Selenium相关的知识,希望对你有一定的参考价值。

Original URL: https://www.guru99.com/use-autoit-selenium.html

How to use AutoIT with Selenium

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

技术分享图片

AutoIt v3 is also freeware. It uses a combination of mouse movement, keystrokes and window control manipulation to automate a task which is not possible by selenium webdriver.

Moving ahead 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

In this tutorial, you will learn-

How to download and install AutoIT

Step 1): Go to this link.

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

技术分享图片

Step 3) Click on ‘AutoIT‘ Downloads option.

技术分享图片

Step 4): Download "Autoit" by clicking on ‘Download Autoit‘ button .

技术分享图片

Step 5): Now download "Autoit editor" by clicking on ‘Downloads‘ button .

技术分享图片

Step 6): Click on the link as shown below.

技术分享图片

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

技术分享图片

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

Step 7): After successfully installation - open up AutoIT Editor.

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

技术分享图片

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

技术分享图片

Step 8) : Now opens element Identifier .

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

技术分享图片

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

技术分享图片

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

Finding element through element Identifier and writing script on AutoIT editor.

Under this, we will see 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 titleclassinstance ) 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.

技术分享图片

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.

技术分享图片

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

技术分享图片

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.

技术分享图片

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

技术分享图片

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.

技术分享图片

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.						

技术分享图片

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.				

技术分享图片

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.

技术分享图片

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.

技术分享图片

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.

技术分享图片

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 ).

技术分享图片

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 ."

技术分享图片

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.

技术分享图片

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

AutoIT Upload file in Selenium Webdriver

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 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.

技术分享图片

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 .

技术分享图片

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("[email protected]");					
    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.

技术分享图片

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 the file uploaded.

This article is contributed by Gaurav Nigam

以上是关于[Selenium+Java] How to use AutoIT with Selenium的主要内容,如果未能解决你的问题,请参考以下文章

[Selenium+Java] How to Take Screenshot in Selenium WebDriver

[Selenium+Java] How to use IntelliJ IDEA & Selenium Webdriver

[Selenium+Java] How to Upload & Download a File using Selenium Webdriver

How to set Selenium Python WebDriver default timeout?

How to click on a point on an HTML5 canvas in Python selenium webdriver

How to Distinguish a Physical Disk Device from an Event Message