Selenium+java上传文件


自动化调用:

  • 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     }

 

使用的时候直接调用就可以

 

 

  

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM