watir autoit 应用

上一篇 / 下一篇  2009-07-09 22:51:03 / 个人分类:RUBY

下面文档,简单罗列了我在技术评估时,经过自己学习,加工整理,总结的文档。由于时间急促不可能涉及方方面面,但常用基础的可以先从下面开始着手。。。,

 

如果期望成为autoIT高手,最好还是参看其help文档,国外有专门招聘auitit自动化,国内很少。

 

1.     AUTOIT

1.1.     AutoIT是什么?

AutoIT是一款自由软件,现在最新版本是autoit v3.

从此介绍上看,其诞生目的就是为了解决某些语言,像vbs and sendkeys,在操作windows gui时不稳定,或无法实现的技术提供解决方案。其主要功能是用来模拟键盘和鼠标来进行windows控件操作来实现操作自动化的目的。与我们要做的自动化相关,具体有如下特征,

A,使用类basic语法脚本

B,模拟键盘和鼠标操作

C,与标准windows控件交互

D,生成GUI

E, com支持

F,正则表达式支持

G,调用外部dllwindows api

H,独立装载,编译,运行,可打包成可执行的exe

 

联系我们做自动化,脱离与iedocument对象的windows对象,watir就力不从心,如:上传,下载,JS写的控件/第三方非web控件alerts window, JS pop up window,几乎所有的非web, windows弹出框都是。

 

从了解watir底层代码,其解决windows对象识别与操作没有用其它技术,就是采用autoit

最后,从搞自动化技术角度来看,autoit是一样非常强大的利器,不使用它非常遗憾。

因为只要在win OS上操作,不管做web、还是windows自动化都需要它帮助,在操作的稳定性

与可能性上autoIT提供了相对较强的解决方案。

  

1.2. AutoIT安装与部署

 

A,下载:

http://www.autoitscrīpt.com/autoit3/downloads.shtml

 

b,注册:

先下载安装,再找到AutoItX3.dll进行手工注册一下,

注意:watir1.5. 6在安装时,其安装目录里提供的AutoItX3.dll似乎不能正常工作

 regsvr32 D:\AutoIt3\AutoItX\AutoItX3.dll

 

c,讨论:

http://www.autoitscrīpt.com/forum/index.php?

 

1.3. AutoIT SPY工具

 

A, autoIT v3 window info工具

非常类似QTPobject spy工具,拖拽到要识别的windows control对象上,

会立即显示Title, class, Advanced(class), ID, ClassnameNN, ControlClick Coords:  

Text等字段。这些属性对于操作windows对象,起到很好的标识作用。供autoiT中内置方法提供操作句柄与操作引用.

 

1.4.     AutoIT scrīpt

 

Auto IT提供一整套的脚本语法,与程序开发规范.

提供特定的数据类型,操作符与保留字,条件语句与函数、子程序结构。

其采用分号;作为脚本注释。

 另外也提供基于命令行的编译与运行环境,不过可以用其自带IDE: sciTE,缺点是

不能debug.

 

下面的操作方法,将会在watir中比较常用到,

 

1,MsgBox(0, "My First scrīpt!", "Hello World!")

2,TestFunc()

Func TestFunc()
    MsgBox(0, "My Second scrīpt!", "Hello from the functions!")
EndFunc

 

3,WinWaitActive ( "title", ["text"], [timeout] )/WinClose("[ACTIVE]", "")

4,ControlSend("Untitled - Notepad", "", "Edit1", "This is some text")/

ControlClick("My Window", "", "[ID:254]")

5,ControlFocus "title", "text", "controlID"

 6,正则StringRegExp("test","pattern"[,flag] )

 7,Send "keys" [, flag]/Sleep delay

 

 

1.5.     应用场景

 

更多的example请参考:autoit3\examples\helpfile\*.*

 1,下载文件

 

def save_file(filepath)

   ai = WIN32OLE.new("AutoItX3.Control")

   ai.WinWait("文件下载", "", 5)

   ai.ControlFocus("文件下载", "", "保存(&S)")

   sleep 1

   ai.ControlClick("文件下载", "", "保存(&S)", "left")

   ai.WinWait("另存为", "", 5)

   sleep 1

   ai.ControlSend("另存为", "", "Edit1",filepath)

   ai.ControlClick("另存为", "", "保存(&S)", "left")

   ai.WinWait("下载完毕", "", 5)

   ai.ControlClick("下载完毕", "", "关闭")

 end

ie.span(:text, "导出Excel").click_no_wait

save_file("C:\\abc.xls")

 

有没有看到,每个autoit方法在call的时候都传入一些参数?

我怎么知道的呢?都是从autoit sby工具获得的。。。

 

2,点击弹出框

 

def check_for_popups

   autoit = WIN32OLE.new('AutoItX3.Control')

   #

   

   # Do forever - assumes popups could occur anywhere/anytime in your application.

   loop do

       # Look for window with given title. Give up after 1 second.

       

       ret = autoit.WinWait('Microsoft Internet Explorer', '', 1)

       #ret = WinActivate("Microsoft Internet Explorer", "")

       autoit.ControlClick("Microsoft Internet Explorer", "", "[CLASS:Button; INSTANCE:1]", 2)

       #强行点击,以使其获得focus

       puts(ret)

       #

       # If window found, send appropriate keystroke (e.g. {enter}, {Y}, {N}).

       

       if (ret==1) then autoit.Send("{Enter}") end

       

       #

       # Take a rest to avoid chewing up cycles and give another thread a go.

       # Then resume the loop.

       sleep(3)

   end

 end

 

 

ie.button(:name, "btnUpload").click_no_wait

sleep(20)

 

$popup = Thread.new { check_for_popups } # start popup handler

 

at_exit { Thread.kill($popup) }

 

还有很多例子,可以以此类推,只要第1:激活,第2步:获得焦点,第3步:就可以sendkey了。

是不是已经搞定99.999%的问题了?

 

注意:在watir中使用autoIT,别忘记require 'win32ole'


TAG:

引用 删除 answer000   /   2011-03-21 18:56:04
最近需要用autoit写个小工具,需要监控程序在window的占用资源(CPU和内存)情况,请问autoit有直接的接口调用吗?
benwu的个人空间 引用 删除 benwu   /   2009-07-13 20:30:11
应该都是没问题的,因为都是用windows的COM接口来实现的,与具体的语言没有关系的,只不过不同的语言调用的方法和方式不一样罢了
前端测试工程师@淘宝 引用 删除 Eric_Lee   /   2009-07-13 10:42:01
好东西,不知道能不能在watin中使用哦?我最近也遇到识别windows pop-up的问题
 

评分:0

我来说两句

日历

« 2024-03-24  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 30182
  • 日志数: 37
  • 建立时间: 2008-07-09
  • 更新时间: 2009-07-23

RSS订阅

Open Toolbar