[英]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.