selenium中应用问题解决
selenium使用Runtime.getRuntime().exec()调用系统执行bat文件
Test.java文件内容:
import java.util.Timer; import java.util.TimerTask; import java.util.Date; public class Test{ public static void main(String[] args){ System.out.println("timer canceled!"); } }
Test.bat文件内容:
java Test>>output.txt
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{ Runtime.getRuntime().exec("javac G:\\temp\\Test.java"); //不支持带有中文的路径 Thread.sleep(3000); // Runtime.getRuntime().exec("notepad.exe"); //调用记事本 //可以调用.bat(内容:java Test>>output.txt)文件执行成功且将打印日志重载到指定文件 Process proc = Runtime.getRuntime().exec("cmd.exe /c start Test.bat",null,new File("G:/temp")); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error"); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output"); errorGobbler.start(); outputGobbler.start(); proc.waitFor(); } }
执行结果:
junit中调用exe文件:
JunitDebug.java
package snippet; import java.io.File; import snippet.util.StreamGobbler; public class JunitDebug{ public void execRuntime() throws Exception{ // Process proc = Runtime.getRuntime().exec("notepad.exe"); Process proc =Runtime.getRuntime().exec("cmd /C update.exe", null, new File("E:/")) ; StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error",null); StreamGobbler inputGobbler = new StreamGobbler(proc.getInputStream(), "Input",null); StreamGobbler outputGobbler = new StreamGobbler(null, "Output", proc.getOutputStream()); errorGobbler.start(); inputGobbler.start(); outputGobbler.start(); proc.waitFor(); } }
JunitDebugTest.java
package snippet; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class JunitDebugTest { private static JunitDebug add = new JunitDebug(); @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() throws Exception { add.execRuntime(); } }
调用记事本(notepad.exe)、update.exe成功
==========================================================================================================================================
testNG中调用exe文件:
JunitDebugTest.java
package snippet.snippet; import org.testng.annotations.Test; import snippet.JunitDebug; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; public class JunitDebugTest { private static JunitDebug add = new JunitDebug(); @BeforeMethod public void beforeMethod() { } @AfterMethod public void afterMethod() { } @BeforeClass public void beforeClass() { } @AfterClass public void afterClass() { } @BeforeTest public void beforeTest() { } @AfterTest public void afterTest() { } @Test public void execRuntime() throws Exception { add.execRuntime(); } }
调用记事本(notepad.exe)、update.exe成功
=======================================================================================================================================
同时或先后启动两个系统exe:
JunitDebug.java
package snippet; import java.io.File; import snippet.util.StreamGobbler; public class JunitDebug{ public void execRuntime() throws Exception{ Process proc = Runtime.getRuntime().exec("notepad.exe"); //success Thread.sleep(3000); Process proc1 = Runtime.getRuntime().exec("calc.exe"); //success StreamGobbler errorGobbler1 = new StreamGobbler(proc1.getErrorStream(), "Error",null); StreamGobbler inputGobbler1 = new StreamGobbler(proc1.getInputStream(), "Input",null); StreamGobbler outputGobbler1 = new StreamGobbler(null, "Output", proc1.getOutputStream()); errorGobbler1.start(); inputGobbler1.start(); outputGobbler1.start(); proc1.waitFor(); } }
JunitDebugTest.java
package snippet.snippet; import org.testng.annotations.Test; import snippet.JunitDebug; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; public class JunitDebugTest { private static JunitDebug add = new JunitDebug(); @BeforeMethod public void beforeMethod() { } @AfterMethod public void afterMethod() { } @BeforeClass public void beforeClass() { } @AfterClass public void afterClass() { } @BeforeTest public void beforeTest() { } @AfterTest public void afterTest() { } @Test public void execRuntime() throws Exception { add.execRuntime(); } }
调用记事本(notepad.exe)、计算器(calc.exe)成功