selenium+AutoIt
上传文件
update.au3文件内容:
执行成功情况一:
;由于Selenium本身的限制,不能驱动windows系统的资源,只能驱动浏览器来实现自动化测试,所以有些页面打开了系统的控件时,需要借助其他工具来实现。AutoIt这个工具正好能做这些事情。 ;*****注意:参数1可以输入控件的titile,但有时候不起作用,不稳定,最好还是使用Class***** ControlFocus("[Class:#32770]","","Edit1") WinWait("[Class:#32770]","",10) ControlSetText("[Class:#32770]","","Edit1","E:\update.xls") Sleep(500) ControlClick("[Class:#32770]","","Button1")
调用结果均成功,但是在eclipse中使用Runtime.getRuntime().exec("exefile")调用转换后的exe文件进程无反馈
=======================================================================================================================================
==================================================================================================================
执行成功情况二:
;由于Selenium本身的限制,不能驱动windows系统的资源,只能驱动浏览器来实现自动化测试,所以有些页面打开了系统的控件时,需要借助其他工具来实现。AutoIt这个工具正好能做这些事情。 ;*****注意:参数1可以输入控件的titile,但有时候不起作用,不稳定,最好还是使用Class***** ;在eclipse中使用Runtime.getRuntime().exec("exefile")调用转换后的exe文件把class标记改为title才可以 ControlFocus("文件上传","","Edit1") WinWait("文件上传","",1000) ControlSetText("文件上传","","Edit1","E:\update.xls") ;ControlFocus("[Class:#32770]","","Edit1") ;WinWait("[Class:#32770]","",10) ;ControlSetText("[Class:#32770]","","Edit1","E:\update.xls") Sleep(500) ControlClick("[Class:#32770]","","Button1")
XX.java文件内容:
package cn.gloryroad; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class StreamGobbler extends Thread { InputStream is; String type; public StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (type.equals("Error")) { System.out.println("Error :" + line); } else { System.out.println("Debug:" + line); } } } catch (IOException ioe) { ioe.printStackTrace(); } } public static void main(String[] args) throws Exception { // System.out.println("=======================java文件编译======================="); // Runtime.getRuntime().exec("javac G:\\temp\\Test.java"); //不支持带有中文的路径success // Thread.sleep(3000); //等待3秒执行编译,然后才能执行编译后的可执行文件 //可以调用.bat(内容:java Test>>output.txt)文件执行成功且将打印日志重载到指定文件 // Process proc = Runtime.getRuntime().exec("cmd.exe /c start Test.bat",null,new File("G:/temp")); //success // System.out.println("=======================调用编译后的java文件,命令("java Test>output.txt")在Test.bat脚本中======================="); // Runtime.getRuntime().exec("notepad.exe"); //调用记事本success // Process proc = Runtime.getRuntime().exec("cmd.exe"); //success Process proc = Runtime.getRuntime().exec("cmd.exe /c start update.exe",null,new File("E:")); //success // Process proc = Runtime.getRuntime().exec("\"E:/update.exe\""); //success // Process proc =Runtime.getRuntime().exec("cmd /C update.exe", null, new File("E:/")) ; //success StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error"); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output"); errorGobbler.start(); outputGobbler.start(); proc.waitFor(); } }
执行结果: