如何使用 Java 以编程方式停止和启动 Appium 服务器?

Smo*_*aky 5 java appium

如何使用 Java 代码启动和停止服务器?目前我正在手动执行此过程。

小智 5

有 3 种方法可以实现该场景。
1) 使用 AppiumDriverLocalService

public void startServer() {
    //Set Capabilities
    cap = new DesiredCapabilities();
    cap.setCapability("noReset", "false");

    //Build the Appium service
    builder = new AppiumServiceBuilder();
    builder.withIPAddress("127.0.0.1");
    builder.usingPort(4723);
    builder.withCapabilities(cap);
    builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
    builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error");

    //Start the server with the builder
    service = AppiumDriverLocalService.buildService(builder);
    service.start();
}

public void stopServer() {
    service.stop();
}
Run Code Online (Sandbox Code Playgroud)


2) 在 Node.exe 中使用 Appium.js

public void startServer() {
    CommandLine cmd = new CommandLine("C:\\Program Files (x86)\\Appium\\node.exe");
    cmd.addArgument("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\Appium.js");
    cmd.addArgument("--address");
    cmd.addArgument("127.0.0.1");
    cmd.addArgument("--port");
    cmd.addArgument("4723");

    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);
    try {
        executor.execute(cmd, handler);
        Thread.sleep(10000);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

public void stopServer() {
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec("taskkill /F /IM node.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)


3) 使用命令提示符启动 Appium 服务器

public void startServer() {
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4723 --session-override -dc \"{\"\"noReset\"\": \"\"false\"\"}\"\"");
        Thread.sleep(10000);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

public void stopServer() {
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec("taskkill /F /IM node.exe");
        runtime.exec("taskkill /F /IM cmd.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }
}<br/>
Run Code Online (Sandbox Code Playgroud)

我发现它很有帮助。希望它有所帮助。来源:http : //www.automationtestinghub.com/3-ways-to-start-appium-server-from-java/