您的位置:首页 > 其它

推荐一个强大便捷的Windows自动化操作工具:AutoIt 3

2011-09-09 15:56 1096 查看


推荐一个强大便捷的Windows自动化操作工具:AutoIt 3

2011-05-28 11:22 91人阅读 评论(0) 收藏 举报

最近在自己整一套Windows下应用程序的自动化测试程序,不想使用诸如QTP的重量级的东西,有人推荐使用AutoIt编写脚本来实现轻量级自动化测试,使用了一下,感觉这个工具确实既强大又方便使用,还可以将脚本程序编译成exe可执行文件,对Windows下的各种窗口、控件的捕捉效果很好,可以模拟鼠标、键盘各种事件,还提供了对大多数win32 API的封装使得调用API更加方便易用,更厉害的是,还可以轻松的实现界面编程,真是相见恨晚。转念一想,这工具可以用于Windows下的自动化操作,例如文件批量命名、文本文件内容抽取、自动点击网页链接、自动安装软件、自动销毁弹出窗口等,所有可以想到的操作,拿了这个工具几分钟手到擒来,呵呵,快哉!

下面举几个例子:

第一个例子:自动抽取文本文件信息(可用于生成代码文件列表)

view
plain

ExtractTextFileInfo("test.txt")

;抽取所有的*.h头文件的第一行,如果编程习惯好,*.h的第一行可以是对该文件的描述

Func ExtractTextFileInfo($output_file_name)

Local $h_search = FileFindFirstFile("*.h") ;打开文件搜索handle

If $h_search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

Local $log_file = FileOpen($output_file_name, 1) ;1表示为追加写入模式

Local $file_name

Local $tmp_str

While 1

$file_name = FileFindNextFile($h_search)

If @error Then ExitLoop

; 读取文件第一行

$tmp_str = "代码文件: " & $file_name & ":"

$tmp_str &= @CRLF & " "

$tmp_str &= FileReadLine($file_name, 1)

$tmp_str &= @CRLF

$tmp_str = StringReplace($tmp_str, "//", " ") ;将代码行里的注释符号"//"替换掉

; 写入Log文件

FileWriteLine($log_file, $tmp_str)

WEnd

FileClose($log_file)

FileClose($h_search) ;关闭文件搜索handle

EndFunc

第二个例子:自动销毁某个进程弹出的对话框

view
plain

Local $pid = 1880

DetectAndDestroyModalDlgByPID($pid)

Local $pid_arr[2] = [1950, 2220]

DetectAndDestroyModalDlgByPID($pid_arr)

; 循环检测并销毁模态对话框:其pid是指定的

Func DetectAndDestroyModalDlgByPID($pid_array)

Local $pop_dlg = DetectModalDlgByPID($pid_array)

While $pop_dlg

WinClose($pop_dlg)

$pop_dlg = DetectModalDlgByPID($pid_array)

WEnd

EndFunc

; 本函数寻找模态对话框[CLASS:#32770]:其pid是指定的

Func DetectModalDlgByPID($pid_array)

Local $dlg_array = WinList("[CLASS:#32770]") ;列出所有对话框

If Not IsArray($dlg_array) Then

Return 0

EndIf

For $i = 1 to $dlg_array[0][0]

Local $dlg_hwnd = $dlg_array[$i][1]

Local $dlg_pid = WinGetProcess($dlg_hwnd)

If $dlg_pid <> -1 Then

If IsArray($pid_array) Then

For $pid_tmp In $pid_array

If $dlg_pid = $pid_tmp Then

Return $dlg_hwnd

EndIf

Next

Else

If $dlg_pid = $pid_array Then

Return $dlg_hwnd

EndIf

EndIf

EndIf

Next

Return 0

EndFunc

第三个例子:自动打开绘图程序mspaint,绘制一个三角形和一个圆形

view
plain

MsPaintCircle()

Func MsPaintCircle()

HotKeySet("{ESC}", "ExitPaintScript") ;注册热键, ESC退出

Run("mspaint.exe")

Local $hwnd = WinWait("[CLASS:MSPaintApp]")

WinActivate($hwnd)

WinSetState($hwnd, "", @SW_MAXIMIZE)

;绘制三角形

MouseClickDrag("left", 200, 200, 600, 200)

MouseClickDrag("left", 600, 200, 400, 500)

MouseClickDrag("left", 400, 500, 200, 200)

;绘制圆形,圆心(400, 300),半径50

$pi = 3.14159265358979

$my_radius = 50;

$my_center_x = 400;

$my_center_y = 300;

$temp_degree = 0;

$last_pos_x = $my_center_x

$last_pos_y = $my_center_y-50

$next_pos_x = 0

$next_pos_y = 0

MouseMove($last_pos_x, $last_pos_y)

While $temp_degree <= 360

$next_pos_x = $my_center_x+$my_radius*Sin($temp_degree*$pi/180)

$next_pos_y = $my_center_y-$my_radius*Cos($temp_degree*$pi/180)

MouseClickDrag("left", $last_pos_x, $last_pos_y, $next_pos_x, $next_pos_y, 1)

$last_pos_x = $next_pos_x

$last_pos_y = $next_pos_y

$temp_degree += 10

WEnd

EndFunc

Func ExitPaintScript()

Exit

EndFunc

第四个例子:使用IE打开网址,获得其文本信息

view
plain

#include <IE.au3>

OpenURLInIE("www.baidu.com")

Func OpenURLInIE($str_url)

Local $hwnd = WinActivate("[CLASS:IEFrame]") ;IE主窗口

Local $ie_obj = _IEAttach($hwnd, "hwnd") ;根据URL关联到IE对象

_IENavigate($ie_obj, $str_url)

Sleep(1000)

Local $htmltxt = _IEBodyReadText($ie_obj)

MsgBox(0, "html", $htmltxt)

Return $htmltxt

EndFunc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: