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)成功

 

转载于:https://www.cnblogs.com/celine/p/11382901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值