推荐一个强大便捷的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
相关文章推荐
- 推荐一个强大便捷的Windows自动化操作工具:AutoIt 3
- 推荐一个wince and windows mobile上的好工具(在PC上的铁掌水上漂)
- 推荐一个写博客的工具Windows Live Writer
- 推荐一个wince and windows mobile上的好工具
- foreman架构的引入1-foreman作为自动化运维工具为什么会如此强大 推荐
- 推荐一个Linux下的Windows Live Messenger(MSN)替代工具
- 推荐一个小型自动化测试工具 - Macro Scheduler
- 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
- 推荐方便好用的Oracle开发工具/客户端工具 - Oracle SQL Handler(国产免费、功能强大、免装客户端、能运行于Windows 7、Linux 及 Mac)
- AutoIt Windows的自动化脚本 推荐
- FFmpeg--一个操作视频音频的强大工具
- [导入][链接] 推荐一个Windows下面的免费系统工具站点 - Sysinternals.com
- 介绍一个windows设备操作工具——devcon.exe
- 推荐一个在线网站seo分析工具:Ahrefs反向链接检查
- 推荐一个yaml文件转json文件的在线工具
- 推荐一个缺陷管理工具,项目管理工具
- 推荐一个可以用u盘安装fedora,ubuntu等Linux的工具
- Paint.NET 2.61 华盛顿州立大学学生用C#做了一个 Paint.NET, 比Windows的MSPaint强大
- windows-linux互传文件便捷操作
- 推荐一个Linux远程桌面工具