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

来源:互联网 发布:aauto python 编辑:程序博客网 时间:2024/06/03 20:44
 

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

 91人阅读 评论(0) 收藏 举报

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

 

下面举几个例子:

 

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

view plain
  1. ExtractTextFileInfo("test.txt")  
  2.   
  3. ;抽取所有的*.h头文件的第一行,如果编程习惯好,*.h的第一行可以是对该文件的描述  
  4. Func ExtractTextFileInfo($output_file_name)  
  5.     Local $h_search = FileFindFirstFile("*.h") ;打开文件搜索handle  
  6.     If $h_search = -1 Then  
  7.         MsgBox(0, "Error""No files/directories matched the search pattern")  
  8.         Exit  
  9.     EndIf  
  10.   
  11.     Local $log_file = FileOpen($output_file_name, 1) ;1表示为追加写入模式  
  12.     Local $file_name  
  13.     Local $tmp_str  
  14.     While 1  
  15.         $file_name = FileFindNextFile($h_search)   
  16.         If @error Then ExitLoop  
  17.           
  18.         ; 读取文件第一行  
  19.         $tmp_str = "代码文件: " & $file_name & ":"  
  20.         $tmp_str &= @CRLF & "        "  
  21.         $tmp_str &= FileReadLine($file_name, 1)  
  22.         $tmp_str &= @CRLF  
  23.         $tmp_str = StringReplace($tmp_str, "//"" ") ;将代码行里的注释符号"//"替换掉  
  24.           
  25.         ; 写入Log文件  
  26.         FileWriteLine($log_file, $tmp_str)  
  27.     WEnd  
  28.   
  29.     FileClose($log_file)  
  30.     FileClose($h_search) ;关闭文件搜索handle  
  31. EndFunc  

 

 

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

 

view plain
  1. Local $pid = 1880  
  2. DetectAndDestroyModalDlgByPID($pid)  
  3. Local $pid_arr[2] = [1950, 2220]  
  4. DetectAndDestroyModalDlgByPID($pid_arr)  
  5.   
  6. ; 循环检测并销毁模态对话框:其pid是指定的  
  7. Func DetectAndDestroyModalDlgByPID($pid_array)  
  8.     Local $pop_dlg = DetectModalDlgByPID($pid_array)  
  9.     While $pop_dlg   
  10.         WinClose($pop_dlg)  
  11.         $pop_dlg = DetectModalDlgByPID($pid_array)  
  12.     WEnd  
  13. EndFunc  
  14.   
  15.   
  16. ; 本函数寻找模态对话框[CLASS:#32770]:其pid是指定的  
  17. Func DetectModalDlgByPID($pid_array)  
  18.     Local $dlg_array = WinList("[CLASS:#32770]") ;列出所有对话框  
  19.     If Not IsArray($dlg_array) Then   
  20.         Return 0  
  21.     EndIf  
  22.       
  23.     For $i = 1 to $dlg_array[0][0]  
  24.         Local $dlg_hwnd = $dlg_array[$i][1]  
  25.         Local $dlg_pid = WinGetProcess($dlg_hwnd)  
  26.         If $dlg_pid <> -1 Then  
  27.             If IsArray($pid_array) Then  
  28.                 For $pid_tmp In $pid_array  
  29.                     If $dlg_pid = $pid_tmp Then   
  30.                         Return $dlg_hwnd  
  31.                     EndIf  
  32.                 Next  
  33.             Else  
  34.                 If $dlg_pid = $pid_array Then  
  35.                     Return $dlg_hwnd  
  36.                 EndIf  
  37.             EndIf  
  38.         EndIf  
  39.     Next  
  40.       
  41.     Return 0  
  42. EndFunc  

 

 

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

view plain
  1. MsPaintCircle()  
  2.   
  3. Func MsPaintCircle()  
  4.     HotKeySet("{ESC}""ExitPaintScript") ;注册热键, ESC退出  
  5.     Run("mspaint.exe")  
  6.     Local $hwnd = WinWait("[CLASS:MSPaintApp]")  
  7.     WinActivate($hwnd)  
  8.     WinSetState($hwnd, "", @SW_MAXIMIZE)  
  9.   
  10.     ;绘制三角形  
  11.     MouseClickDrag("left", 200, 200, 600, 200)  
  12.     MouseClickDrag("left", 600, 200, 400, 500)  
  13.     MouseClickDrag("left", 400, 500, 200, 200)  
  14.   
  15.     ;绘制圆形,圆心(400, 300),半径50  
  16.     $pi = 3.14159265358979  
  17.     $my_radius = 50;  
  18.     $my_center_x = 400;  
  19.     $my_center_y = 300;  
  20.     $temp_degree = 0;  
  21.   
  22.     $last_pos_x = $my_center_x  
  23.     $last_pos_y = $my_center_y-50  
  24.     $next_pos_x = 0  
  25.     $next_pos_y = 0  
  26.     MouseMove($last_pos_x, $last_pos_y)  
  27.   
  28.     While $temp_degree <= 360   
  29.         $next_pos_x = $my_center_x+$my_radius*Sin($temp_degree*$pi/180)  
  30.         $next_pos_y = $my_center_y-$my_radius*Cos($temp_degree*$pi/180)  
  31.         MouseClickDrag("left", $last_pos_x, $last_pos_y, $next_pos_x, $next_pos_y, 1)  
  32.         $last_pos_x = $next_pos_x  
  33.         $last_pos_y = $next_pos_y  
  34.         $temp_degree += 10  
  35.     WEnd  
  36. EndFunc  
  37.   
  38.   
  39. Func ExitPaintScript()  
  40.     Exit  
  41. EndFunc  

 

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

view plain
  1. #include <IE.au3>  
  2.   
  3. OpenURLInIE("www.baidu.com")  
  4.   
  5. Func OpenURLInIE($str_url)  
  6.     Local $hwnd = WinActivate("[CLASS:IEFrame]") ;IE主窗口  
  7.     Local $ie_obj = _IEAttach($hwnd, "hwnd") ;根据URL关联到IE对象  
  8.     _IENavigate($ie_obj, $str_url)  
  9.     Sleep(1000)  
  10.     Local $htmltxt = _IEBodyReadText($ie_obj)  
  11.     MsgBox(0, "html", $htmltxt)  
  12.     Return $htmltxt  
  13. EndFunc  
  14.