1

我正在尝试使用 java 自动化我的 selenium 脚本。我试图执行的命令是:

/usr/bin/java -jar /javalibs/selenium-server/selenium-server-standalone-2.32.0.jar -htmlSuite "*firefox" "http://www.google.com" "/tmp/googlesearchsuite.html" "/tmp/RESUTL.html"

该命令在我的 Mac 终端上完美执行。现在我想把它转换成Java;这是我为此使用的代码:

public void localRun() {

    Process p = null;

    try {

        String cmd = "/usr/bin/java -jar /javalibs/selenium-server/selenium-server-standalone-2.32.0.jar -debug -htmlSuite \"*firefox\" \"http://www.google.com\" \"/tmp/googlesearchsuite.html\" \"/tmp/RESUTL.html\"";

        System.out.print("COMMAND: " + cmd);

        System.out.println();

        Runtime runtime = Runtime.getRuntime();
        p = runtime.exec(cmd);
        p.waitFor();

        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        String line = null;

        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }

        line = null;

        while ((line = error.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = p.waitFor();

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    } finally {
        p.destroy();
    }
}

当我运行该 java 代码时,我收到了带有奇怪错误消息的日志(省略了整个日志):

testSuite=/lensoo/googlesearchsuite.html
COMMAND: /usr/bin/java -jar /javalibs/selenium-server/selenium-server-standalone-2.32.0.jar -debug -htmlSuite "*firefox" "http://www.google.com" "/lensoo/googlesearchsuite.html" "/lensoo/RESUTL.html"
14:49:55.619 INFO - Java: Apple Inc. 20.45-b01-451
14:49:55.620 INFO - OS: Mac OS X 10.8.3 x86_64
14:49:55.630 INFO - v2.32.0, with Core v2.32.0. Built from revision 6c40c18
14:49:55.631 INFO - Selenium server running in debug mode.
......
14:49:55.790 INFO - Started HttpContext[/wd,/wd]
14:49:55.801 INFO - Started SocketListener on 0.0.0.0:4444
14:49:55.801 INFO - Started org.openqa.jetty.jetty.Server@152c7568
May 3, 2013 2:49:49 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
Can't find HTML Suite file:/Users/maksim/IdeaProjects/CMPE287-TaaS/"/tmp/googlesearchsuite.html":
Usage: java -jar selenium-server.jar [-interactive] [options]

  -port <nnnn>: the port number the selenium server should use
    (default 4444)
  -timeout <nnnn>: an integer number of seconds we should allow a
    clie.......

这是我收到的全部信息:http: //pastebin.com/QzHeQ0Bm

从命令行和java执行命令有什么区别吗?如果是,那么执行该命令以运行我的 Selenium HTML 套件的正确方法是什么。

4

1 回答 1

2

您应该将 String[] 作为第二个参数传递给您的运行。改变它,我相信它会起作用。

String cmd ="/usr/bin/java -jar /javalibs/selenium-server/selenium-server-standalone-2.32.0.jar"
String[] args = {"my","-arguments","-to","pass","in"};
p = runtime.exec(cmd, args);

我还找到了这个答案,Passing arguments form java program to bash script that call another java programa with the arguments。其中显示了运行命令的几种不同方式。

于 2013-05-03T23:04:11.220 回答