[英]Starting appium server through Java
我正在使用MAC终端启动appium服务器。 在终端我执行了命令appium &
启动正在运行的服务器。
我使用npm -g install appium
通过终端安装了appium服务器
但是,当我尝试使用Java执行相同的代码时,服务器无法启动。
码:
Runtime.getRuntime().exec(new String[]{"/bin/sh","appium &"})
错误:没有这样的文件或目录。
我还尝试使用appium命令创建shell脚本。 当我通过Java调用shell脚本时,它表示找不到命令。
用于调用shell脚本命令的代码。
Process p = new ProcessBuilder(new String[]{"/bin/sh","-c","sh appium.sh"})
在Java中调用时,它会给出错误“line1上的appium.sh:Error - 找不到appium命令”
当我通过终端调用相同的shell脚本时,appium服务器已成功启动。
您可以使用以下代码使用java代码启动appium服务器,并在初始化appium 驱动程序时使用service_url。示例来自此POST
import java.io.File;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
public class AppiumServerStartStop {
static String Appium_Node_Path="C:\\Program Files (x86)\\Appium\\node.exe";
static String Appium_JS_Path="C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js";
static AppiumDriverLocalService service;
static String service_url;
public static void appiumStart() throws Exception{
service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).
withAppiumJS(new File(Appium_JS_Path)));
service.start();
Thread.sleep(25000);
service_url = service.getUrl().toString();
}
public static void appiumStop() throws Exception{
service.stop();
}
}
public static void startAppiumServer() {
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final String appiumNodeFilePath = APPIUM_NODE_FILE_PATH;
final String appiumJavaScriptServerFile = APPIUM_JAVA_SCRIPT_SERVER_FILE_PATH;
final String appiumServerPortNumber = APPIUM_SERVER_PORT_NUMBER;
final String appiumServerConfigurations = "--no-reset --local-timezone --port "+ appiumServerPortNumber+ " -bp "+(Integer.parseInt(appiumServerPortNumber)+1);
(new Thread(){
public void run(){
String startCommand ="\"" + appiumNodeFilePath + "\" \""+ appiumJavaScriptServerFile + "\" "+ appiumServerConfigurations;
System.out.println("Server start command: "+startCommand);
executeCommand(startCommand);
}
}).start();
try {
Thread.sleep(25000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void executeCommand(String command) {
String s = null;
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Appium Server Output Logs:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Appium Server Error Logs:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
System.out.println("exception: ");
e.printStackTrace();
}
}
APPIUM_NODE_FILE_PATH =“C:\\ Program Files(x86)\\ Appium \\ node.exe”; APPIUM_JAVA_SCRIPT_SERVER_FILE_PATH =“C:\\ PROGRAMFILES(x86)的\\ Appium \\ node_modules \\ appium \\ BIN \\ appium.js
如果您有兴趣了解它应该如何实现,请检查appium-java-client AppiumDriverLocalService类。
由于您在大多数情况下使用Java,因此最好使用AppiumDriverLocalService
来实现您自己的解决方案:
AppiumDriverLocalService service = AppiumDriverLocalService.
buildDefaultService();
service.start() // to start appium server
...
service.getUrl() // to get URL of running server
...
service.isRunning() // to check if appium server is alive
...
service.stop() // to stop appium server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.