自動化調用:
- AutoIT腳本編譯成可執行文件后,放在本地的某一個目錄下
- 上傳文件時,首先定位到【上傳】字樣文本,點擊該按鈕
- 執行編輯后的可執行文件,實現文件上傳
一、安裝AutoIT3,主要用到的工具
1 AutoIt Windows Info 用於幫助我們識Windows控件信息。 2 3 Compile Script to.exe 用於將AutoIt生成 exe 執行文件。 4 5 Run Script 用於執行AutoIt腳本。 6 7 SciTE Script Editor 用於編寫AutoIt腳本。
1)、用AutoIt Windows Info 識別Windows控件信息,如按鈕
2、用SciTE Script Editor編寫腳本,腳本內容如下:
1 ;first make sure the number of arguments passed into the scripts is more than 1 2 If $CmdLine[0]<2 Then Exit EndIf ;if parmas num <2 ,then break 3 ;$CmdLine[0] ;參數的數量 4 ;$CmdLine[1] ;第一個參數 (腳本名稱后面) 5 ;$CmdLine[2] ;第二個參數 6 ;都是從cmd傳入參數 7 handleUpload($CmdLine[1],$CmdLine[2]) 8 9 ;定義上傳函數,有兩個參數,第一個是瀏覽器名字,第二參數是文件路徑 10 Func handleUpload($browser, $uploadfile) 11 Dim $title ;定義一個title變量 12 ;根據彈窗的title來判斷是什么瀏覽器 13 If $browser="ie" Then ; 代表IE瀏覽器 14 $title="選擇要加載的文件" 15 ;ElseIf $browser="chrome" Then ; 代表谷歌瀏覽器 16 ; $title="打開" 17 ElseIf $browser="firefox" Then ; 代表火狐瀏覽器 18 $title="文件上傳" 19 EndIf 20 21 if WinWait($title,"",4) Then ;等待彈出出現,最大等待時間是4秒 22 WinActivate($title) ;找到彈出窗口之后,激活當前窗口 23 ControlSetText($title,"","Edit1",$uploadfile);把文件路徑放入輸入框,此Edit1是用FinderTool獲取到的 24 ControlClick($title,"","Button1");點擊保存或者打開或者上傳按鈕,此Button1使用FinderTool獲取到的 25 Else 26 Return False 27 EndIf 28 EndFunc
說明:title是AutoIt Window Info識別出的Title字段,controlID是AutoIt Window Info識別出的Class和Instance的拼接
運行腳本,Toos-->Go,運行時,上傳窗口處於打開狀態。
3)、用Complie Script to .exe工具,將腳本編譯成可執行文件,即編譯成.exe格式
二、執行AutoIT編寫的.exe文件
1 /* 2 * 上傳文件 3 * 普通上傳:普通的附件上傳是將本地文件的路徑作為i一個值放在input標簽中,通過form表單將這個值提交給服務器 4 */ 5 //定義上傳函數,第一個參數browser是瀏覽器的名字,第二個參數filePath是文件路徑 6 public void handleUpload(String browser,String filePath) { 7 //定義了autoit.ext文件的路徑 8 String executeFile=System.getProperty("user.dir")+"\\test_exe\\autoit.exe"; 9 String cmd = "\""+ executeFile+ "\""+ " "+ "\""+ browser+ "\""+ " "+ "\""+ filePath+ "\""; 10 System.out.println(cmd); 11 try{ 12 Process process= Runtime.getRuntime().exec(cmd); 13 process.waitFor(); 14 } catch(Exception e) { 15 e.printStackTrace(); 16 } 17 } 18 19 /** 20 * 導入文件 21 * */ 22 public void ExcelImport(String filePath){ 23 browse_m.click(); //@FindBy(name="get_file") public WebElement browse_m;//點擊瀏覽按鈕 24 handleUpload(DriverManager.browserType,filePath); 25 }
使用的時候直接調用就可以