使用AutoIt實現UI自動化上傳附件

1、下載AutoIt,https://www.autoitscript.com/site/autoit/downloads/(這個地址下載下來的不好使的話,自行搜索下載,CSDN上有很多)

2、找到安裝後的文件路徑,打開autoit窗口信息工具

3、點擊要上傳圖片的按鈕,打開文件選擇框,注意,一定要打開windows的文件選擇框

4、將控件探測工具分別拖至選擇圖片窗口的文件名輸入框以及打開按鈕處,記錄需要的類等信息



5、打開autoit的sci te編輯腳本,輸入;另存爲au3文件

WinActivate("打開");
;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("打開", "","Edit1")

; Wait 10 seconds for the Upload window to appear
  WinWait("打開","[CLASS:#32770]",10)

; Set the File name text on the Edit field
  ControlSetText("打開", "", "Edit1", "C:\Users\Administrator\Desktop\1.jpg")

  Sleep(2000)

; Click on the Open button
  ControlClick("打開", "","Button1");



參數化的寫法:

WinWaitActive($CmdLine[1])
WinActive($CmdLine[1])
ControlSetText($CmdLine[1],"","[CLASS:Edit;INSTANCE:1]",$CmdLine[2])
ControlClick($CmdLine[1],"","[CLASS:Button;INSTANCE:1]")
Send("!o")


 附註:
  •     ControlFocus ( "title", "窗口文本", controlID)   設置輸入焦點到指定窗口的某個控件上;
  •     WinWait ( "title" , "窗口文本" , 超時時間 )  暫停腳本的執行直至指定窗口存在(出現)爲止;
  •     ControlSetText ( "title", "窗口文本", controlID, "新文本" )   修改指定控件的文本;
  •     Sleep ( 延遲 )   使腳本暫停指定時間段;
  •     ControlClick ( "title", "窗口文本", 控件ID , 按鈕 點擊次數 )   向指定控件發送鼠標點擊命令;

    其中,titleAutoIt Window Info識別出的Title字段,controlIDAutoIt Window Info識別出的ClassInstance的拼接,如上圖拼接後的結果應爲:Button1;


6、驗證腳本是否正確,點擊頁面的上傳圖片按鈕,打開上傳圖片對話框;切換到sci te編輯腳本窗口,點擊工具菜單----執行腳本,注意觀察下面的日誌,執行完畢後查看頁面是否已經上傳好圖片



7、打開auit編譯腳本工具,打開上一步保存的au3文件,轉換成exe文件



8、selenium中調用exe程序

Runtime r = Runtime.getRuntime();
//因爲工程文件放在了C盤下,直接訪問res文件夾下的exe文件會拒絕訪問,暫時改成電腦E盤下的可執行文件調用
String scriptFile = "E:/test.exe";
@SuppressWarnings("unused")
Process pro = null;
try {
    pro = r.exec(scriptFile);
    browserEmulator.pause(15000);
} catch (IOException e) {
    e.printStackTrace();

}


參數化寫法的調用:

Runtime r = Runtime.getRuntime();
String scriptFile = "./res/AutoItScript/upload.exe";
@SuppressWarnings("unused")
Process pro = null;
   try {
      pro = r.exec(scriptFile+" "+windowsTitle+" "+fileNameWithPath);
      logger.info("upload file:" + fileNameWithPath);;
   } catch (IOException e) {
      e.printStackTrace();
      logger.error("Run uploadfile.exe fail");
   }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章