[Selenium+java] How to use AutoIT with Selenium

Source: Internet
Author: User
Tags file upload script guru99

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

How to use AutoIT with Selenium

Selenium is a open source tool that's designed to automate web-based applications on different browsers and to handle WI Ndow GUI and non HTML popups in application. AutoIT is required as these window based activity was 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 was not possib Le by selenium webdriver.

Moving ahead we'll 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's want to automate

In this tutorial, you'll learn-

    • How to AutoIT download and install

    • finding element through element Identifier and writing script on AutoIT Editor.

    • AutoIT Upload file in Selenium webdriver

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'll get II Setup file as shown in below screens, first is AutoIt version 3 setup and second I S Scitautoit3 .

Step 6): For installing Autoit-click in 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.

Note: Once you do with this element identifier your need to close manually, it'll not close automatically.

finding element through element Identifier and writing script on AutoIT Editor.

Under this, we'll see what the 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 Ele ment like title, class, instance ) and how to write the script on AutoIT editor using 3 methods.

For Example: We'll use the "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 Uplo Ading part.

Runtime.getruntime (). EXEC ("E:\\autoit\\fileupload.exe");

Finally, when we run selenium script-it would 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.

Step 2): Now Open File Uploader window by clicking in ' 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 attribute s info as shown in the below screens with the arrow.

We can get the value of attributes i.e. title= ' Open ', class= ' Edit ' and instance= ' 1 ' as shown be Low. These values is 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 S Hown in step 7 from the 1st topic.

Start writing a script for selecting a file to upload.

There is lots of method available which we can use with a script according to the requirement, but right now we'll focus On the below methods as these methods is 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 is displayed as shown in below. The good feature of AutoIT is that it's somewhat like Eclipse this 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 was already identified in above step 3. We can open it by maximizing it.

Now, we'll take the values from element identifier to ' controlfocus ' and ' Controlsettext ' methods as these methods work s 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 was used to define the path of a file which we need to upload in ' File name ' text box. In another-on-the-say, we can-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 the Open File Uploader window as to the 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 attribut E information.

Previous values (i.e attributes of ' File name ' text box) overwrite with new values of ' Open ' button. Can see the class attribute are now changed to "button" which were 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 is 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 screens that AutoIT script are completed to handle file uploader. Now your can close the element identifier and save the script as "FileUpload" at the given location (E:\AutoIT).

Now the can ' t execute this script directly, the need to compile this script.

For compiling this script, you have the options " compile script x64 " and " compile script x86 ", if yo You have a Windows 32-bit machine then u go with " compile script x86" and a 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. Now we can use the this file in Selenium webdriver script.

Now we'll use the 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 the 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 Contr Ol send 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 would 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 would 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 was contributed by Gaurav Nigam

[Selenium+java] How to use AutoIT with Selenium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.