Autoit

作者: halfempty | 来源:发表于2019-03-13 15:06 被阅读0次

转自: https://www.guru99.com/use-autoit-selenium.html

How to use AutoIT with Selenium Webdriver: File Upload Example

What is AutoIt?

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.

In this tutorial, you will learn-

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

How to use AutoIT with Selenium

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

How to download and install AutoIT

Step 1): Go to this link.

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

How to use AutoIT with Selenium

Step 3) Click on 'AutoIT' Downloads option.

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

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'

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

Step 8) : Now opens element Identifier .

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

How to use AutoIT with Selenium

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

How to use AutoIT with Selenium

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

<pre style="box-sizing: inherit; overflow: auto; margin: 0px 0px 1.5rem; padding: 0.938rem; font-size: 13px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; background: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 0.1875rem; line-height: 20px; display: block; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap;">Runtime.getRuntime().exec("E:\AutoIT\FileUpload.exe"); </pre>

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

How to use AutoIT with 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.

How to use AutoIT with Selenium

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

How to use AutoIT with 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.

How to use AutoIT with 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 as explained in below step 4.

How to use AutoIT with 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.

How to use AutoIT with 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.'

    <pre style="box-sizing: inherit; overflow: auto; margin: 0px 0px 1.5rem; padding: 0.938rem; font-size: 13px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; background: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 0.1875rem; line-height: 20px; display: block; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap;">ControlFocus("Open","","Edit1") // This method sets input focus to 'File name' text box. </pre>

How to use AutoIT with 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.

    <pre style="box-sizing: inherit; overflow: auto; margin: 0px 0px 1.5rem; padding: 0.938rem; font-size: 13px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; background: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 0.1875rem; line-height: 20px; display: block; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap;">ControlSetText("Open","","Edit1","E:\Resume\resume.doc") // This method input file path of a control. </pre>

How to use AutoIT with 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.

How to use AutoIT with 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.

How to use AutoIT with 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 ".

<pre style="box-sizing: inherit; overflow: auto; margin: 0px 0px 1.5rem; padding: 0.938rem; font-size: 13px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; background: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 0.1875rem; line-height: 20px; display: block; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap;">ControlClick("Open","","Button1") //This method click on 'Open' button of file uploader.</pre>

How to use AutoIT with 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 ).

How to use AutoIT with 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 ."

How to use AutoIT with 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.

How to use AutoIT with Selenium

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.

How to use AutoIT with Selenium

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

<pre style="box-sizing: inherit; overflow: auto; margin: 0px 0px 1.5rem; padding: 0.938rem; font-size: 13px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; background: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 0.1875rem; line-height: 20px; display: block; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap;">Runtime.getRuntime().exec("E:\AutoIT\FileUpload.exe");</pre>

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

How to use AutoIT with Selenium

Step 2) : Execute the Selenium script in Eclipse.

import java.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](mailto: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.

How to use AutoIT with Selenium

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.

相关文章

  • PyAutoIt

    一、简介 PyAutoIt包提供了AutoIt的python接口。AutoIt是用来针对window程序进行自动化...

  • AutoIt工具使用

    AutoIt语法 注释 脚本顺序 获取弹框标题(AutoIt Window Info(窗口信息工具) 尝试编写第一...

  • Autoit

  • Autoit

    转自: https://www.guru99.com/use-autoit-selenium.html How t...

  • selenium借用AutoIt 实现上传文件,selenium

    1、AutoIT介绍 AutoIT是一个类似脚本语言的软件,利用此软件我们可以方便的实现模拟键盘、鼠标、窗口等操作...

  • AutoIt笔记

    下载地址:https://www.autoitscript.com/site/autoit/downloads/ ...

  • AutoIT自动化测试入门(4)-- 自定义无参函数

    本期要为大家带来的是AutoIT自定义无参函数,之前已经介绍过了AutoIT中有很多自带的函数可以方便我们做一些事...

  • AutoIT函数

    Abs 求某个数的绝对值.ACos 求某个数的反余弦值(arcCosine).AdlibRegister 注册一个...

  • 重温AutoIt

    好几年前出于利用现有GUI进行测试的目的,接触了AutoIt 最近再次遇到相同的需求整合现有的GUI工具进行自动化...

  • autoit使用

    AutoIt Window Info: 用于识别Windows控件信息Compile Script to.exe:...

网友评论

      本文标题:Autoit

      本文链接:https://www.haomeiwen.com/subject/cfxtmqtx.html