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();@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@AfterClasspublic static void tearDownAfterClass() throws Exception {}@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic 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();@BeforeMethodpublic void beforeMethod() {}@AfterMethodpublic void afterMethod() {}@BeforeClasspublic void beforeClass() {}@AfterClasspublic void afterClass() {}@BeforeTestpublic void beforeTest() {}@AfterTestpublic void afterTest() {}@Testpublic 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"); //successStreamGobbler 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();@BeforeMethodpublic void beforeMethod() {}@AfterMethodpublic void afterMethod() {}@BeforeClasspublic void beforeClass() {}@AfterClasspublic void afterClass() {}@BeforeTestpublic void beforeTest() {}@AfterTestpublic void afterTest() {}@Testpublic void execRuntime() throws Exception {add.execRuntime();} }調用記事本(notepad.exe)、計算器(calc.exe)成功
?
轉載于:https://www.cnblogs.com/celine/p/11382901.html
總結
以上是生活随笔為你收集整理的selenium中应用问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc登陆示例(非注解)
- 下一篇: 启用sql日志