java + selenium测试框架(之上传下载) 版本演化五

java + selenium测试框架(之上传下载) 版本演化五

使用AutoIt可以完成文件的上传下载,只需要在Java中进行调用即可:

Auto脚本

_UploadFile

Func _UploadFile($file)
    AutoItSetOption("WinTitleMatchMode", 2)
    WinWaitActive("","文件名", 15);
    ControlSetText("", "文件名", "[CLASS:Edit; INSTANCE:1]", $file);
    ControlClick("", "文件名","[CLASS:Button; TEXT:打开(&O)]");
EndFunc
_UploadFile($CmdLine[$CmdLine[0]])

_DownloadFile

Func _DownloadFile($file)
    AutoItSetOption("WinTitleMatchMode", 2);
   WinWaitActive("另存为")
   ControlSetText("","","[CLASS:Edit; INSTANCE:1]",$file);
   ControlClick("", "","[CLASS:Button; INSTANCE:1]");

   WinWaitActive("确认另存为","",2);
   ;如果该路径下存在文件,则替换
   If WinExists("确认另存为") Then
      ControlClick("确认另存为","","[CLASS:Button; INSTANCE:1]");
   EndIf
EndFunc
__DownloadFile($CmdLine[$CmdLine[0]])

Java调用

public class AutoScriptUtil {
    private static String autoItExecutablePath;

    private static String autoItScriptPath;

    public void FileLoading(String filepath,int timeTowait,boolean isUpLoad) {
        String ScriptPath = null;
        try {               
            String[] scriptcmd = new String[] { autoItExecutablePath, autoItScriptPath, filepath };
            Runtime.getRuntime().exec(scriptcmd);
            // 等待AutoIt脚本开始执行
            Thread.sleep(timeTowait);
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }   
}

你可能感兴趣的:(java,框架,selenium,autoit)