博学而笃定,切问而近思

Autoit工具在selenium平台上解决上传和下载问题

上一篇 / 下一篇  2013-12-11 17:03:02 / 个人分类:Autoit3

这几天在selenium自动化的过程中遇到文件上传,下载的问题
autoit工具是可以识别多数的Windows GUI,可以发生一些用户的世界操作,就这样我们用它的原因

首先下载autoit
可以autoit官网上下载,安装 http://www.autoitscript.com/site/
autoit 在线文档,是中文版http://www.autoitx.com/Doc/


1. 上传文件的*.au3
//其中Open是你要指定的窗口title
WinWait("Open")
WinActivate("Open")
if WinExists("Open") Then 
   ControlFocus("Open","","[CLASS:Edit; Instance:1]")
   ControlSend("Open","","[CLASS:Edit; Instance:1]","C:\Users\chenchen\Pictures\no path.png") //C:\Users\chenchen\Pictures\no path.png 指定上传文件
  
   ControlClick("Open","","[CLASS:Button; Instance:1]")
   
EndIf
;需要将激活ff浏览器窗口【注意点的第3点】
WinWait("Class:MozillaWindowClass")
WinActivate("Class:MozillaWindowClass")

2. 接下来是selenium怎么调用autoit脚本
public void uploadFilesByAutoIt(WebDriver dr) throws InterruptedException, IOException{
dr.findElement(By.id("upload_button")).click();
Thread.sleep(3000);
dr.findElement(By.id("fileImage")).click();
Thread.sleep(2000);
//upload file with autoit
executeUploadByAutoItSC();
Thread.sleep(2000);
driver.findElement(By.id("fileSubmit")).click();
Thread.sleep(5000);
}

public void executeUploadByAutoItSC() throws IOException{
Runtime rt= Runtime.getRuntime();
try{
rt.exec("C:\\Autoit\\AutoIt3\\AutoIt3.exe E:\\study_autotest\\maven\\ssdemo\\src\\TestData\\autoit\\uploadFile.au3");
}catch(Exception e){
System.out.println(e.getMessage());
}
}

另外是是下载的举例
public void downloadFile(WebDriver dr) throws IOException, InterruptedException{
dr.findElement(By.linkText("Download")).click();
Thread.sleep(3000);
 
//download with autoit
executeDownloadByAutoItSC();
}

public void executeDownloadByAutoItSC() throws IOException{
Runtime rt = Runtime.getRuntime();
try {
 
rt.exec("C:\\Autoit\\AutoIt3\\AutoIt3.exe E:\\study_autotest\\maven\\ssdemo\\src\\TestData\\autoit\\downloadFile.au3");
}catch(Exception e){
System.out.println(e.getMessage());
}
}

需要注意的是3点:
1. 如果AutoIt3.exe的路径中有空格,需要用引号括起来
rt.exec("C:\\Program Files\\AutoIt3\\AutoIt3.exe" E:\\study_autotest\\maven\\ssdemo\\src\\TestData\\autoit\\downloadFile.au3"); 这个我也是从网上看到,我遇到的这样的问题,但是用引号无效不知为何,无奈重新安装位置

2. 在没有引用*.au3前,先确保脚本能够跑通,功能正常!这样出现问一步步排查
3. 个人的探索或学习:如果用autoit上传文件,在au3脚本运行最后需要激活浏览器窗口一遍selenium driver的后续操作,我在不同的机器上看到,有时不要重新定位,就能继续后续操作,有时不行,



TAG:

 

评分:0

我来说两句

Open Toolbar