我使用以下代码片段通过mac中的java代码启动appium服务器。
public void startAppium() throws ExecuteException, IOException, InterruptedException {
CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node"); command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
command.addArgument("--address", false);
command.addArgument("127.0.0.1");
command.addArgument("--port", false);
command.addArgument("4723");
command.addArgument("--no-reset", false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
Thread.sleep(50000);
System.out.println("Appium Server starting");
}
我在@BeforeTest中调用上面的方法。在@BeforeMethod内部,我提到了所需的功能。服务器启动并且它也正在获取所需的功能,但它显示以下日志并显示错误:
e[36minfoe[39m: e[37m-->e[39m e[37mPOSTe[39m e[37m/wd/hub/sessione[39m e[90m{"desiredCapabilities":{"app":"/Users/aaaa/dd/qd.apk","appPackage":"com.xxx.xxx","appActivity":"com.xxx.xxx.MainActivity","newCommandTimeout":"1200","platformName":"Android","deviceName":"Android Device"}}e[39m
e[36minfoe[39m: Client User-Agent string: Apache-HttpClient/4.5.2 (Java/1.8.0_73)
e[36minfoe[39m: [debug] Using local app from desired caps: /Users/aaaa/dd/qd.apk
e[36minfoe[39m: [debug] Creating new appium session 6a2245bd-d1ac-443c-bf5f-c6e2c2f64e22
e[36minfoe[39m: Starting android appium
e[36minfoe[39m: [debug] Getting Java version
e[36minfoe[39m: [debug] Cleaning up android objects
e[36minfoe[39m: [debug] Cleaning up appium session
e[31merrore[39m: Failed to start an Appium session, err was: Error: 'java -version' failed. Error: Command failed: /bin/sh -c java -version /bin/sh: java: command not found
e[36minfoe[39m: [debug] Error: 'java -version' failed. Error: Command failed: /bin/sh -c java -version
/bin/sh: java: command not found
我已经尝试过将JAVA_HOME设置为项目的环境变量,但没有用。
任何人都可以提出这方面的建议。谢谢。
答案 0 :(得分:0)
//To start appium call this function
public void startAppium() throws Exception {
String port = getPort();
String chromePort = getPort();
String bootstrapPort = getPort();
String command = "appium --session-override -p " + port + " --chromedriver-port " + chromePort + " -bp "
+ bootstrapPort;
System.out.println(command);
String output = runCommand(command); //run command on terminal
System.out.println(output);
}
// This function will run command on terminal
public String runCommand(String command) throws InterruptedException, IOException
{
p = Runtime.getRuntime().exec(command);
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line="";
String allLine="";
while((line=r.readLine()) != null){
allLine=allLine+""+line+"\n";
if(line.contains("started on"))
break;
}
return allLine;
}
//This will check for free ports
public String getPort() throws Exception
{
ServerSocket socket = new ServerSocket(0);
socket.setReuseAddress(true);
String port = Integer.toString(socket.getLocalPort());
socket.close();
return port;
}