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

时间:2016-01-12 13:44:14

标签: java appium

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

5 个答案:

答案 0 :(得分:2)

顶部提供的 AppiumDriverLocalService 解决方案解决了我手动启动服务器时不断收到的 Socket Hang Up 错误,这是我永无止境的问题。

这是我的代码中的样子:

public final class AppiumServiceProvider {

    private final static Logger log = Logger.getLogger(AppiumServiceProvider.class);
    private static AppiumDriverLocalService service = null;
    private static final TestConfiguration testConfiguration = new TestConfiguration();

    private AppiumServiceProvider() {
    }

    public static AppiumDriverLocalService getService() {
        if (service == null) {
            initAppiumService();
        }
        if (service == null) {
            fail("Cannot start Appium Driver Local Service.");
        }
        return service;
    }

    private static void initAppiumService() {
        Map<String, String> env = new HashMap<>(System.getenv());
        env.put("PATH", "/usr/local/bin:" + env.get("PATH"));
        env.put("ANDROID_HOME", "/Users/nets/Library/Android/sdk");

        service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                .usingAnyFreePort()
                .withAppiumJS(new File(testConfiguration.getAppiumJsLocation()))
                .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
                .withArgument(GeneralServerFlag.SESSION_OVERRIDE)
                .withArgument(GeneralServerFlag.LOG_LEVEL, "error")
                .withArgument(GeneralServerFlag.RELAXED_SECURITY)
                .withEnvironment(env)
                .withLogFile(new File("target/appium.log"))
                .withStartUpTimeOut(60, TimeUnit.SECONDS));

        log.info("New Appium service: " + service.getUrl());
        service.start();
    }
}

答案 1 :(得分:1)

请阅读此内容 - 它将说明如何启动https://github.com/appium/java-client/pull/240

最新的java客户端有api来执行此操作。

这是一种以编程方式开始的方式,页面列出了更多这样的方式。

        AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
        final DesiredCapabilities capabilities = new DesiredCapabilities();
        // capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");
        capabilities.setCapability("deviceName", "MiPad");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("automationName","Appium");
        //            capabilities.setCapability("avd","firstavd");
        capabilities.setCapability("appPackage", "com");
        capabilities.setCapability("appActivity", ".ui.activity");
        driver = new AndroidDriver<MobileElement>(service, capabilities);

答案 2 :(得分:1)

对于那些正在使用npm(node / js / typescript)的人来说,我创建了一个名为appium-controller的模块,它以编程方式在后台启动和停止appium(mac或windows)。

答案 3 :(得分:1)

有三种方法可以实现这种情况 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();
}


2)将Appium.js与Node.exe一起使用

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();
    }
}


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/>

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

答案 4 :(得分:0)

尝试一下-对我有用

示例代码:

public static AppiumDriverLocalService startAppiumServer() {
    boolean flag=   checkIfServerIsRunnning(4723);
    if(!flag)
    {
        AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder();
        // Use any port, in case the default 4723 is already taken (maybe by another Appium server)
        serviceBuilder.usingAnyFreePort();
        // Tell serviceBuilder where node is installed. Or set this path in an environment variable named NODE_PATH
        serviceBuilder.usingDriverExecutable(new File("/Users/Puneetha/.nvm/versions/node/v11.10.1/bin/node"));
        // Tell serviceBuilder where Appium is installed. Or set this path in an environment variable named APPIUM_PATH
        serviceBuilder.withAppiumJS(new File("/Users/Puneetha/.nvm/versions/node/v11.10.1/bin/appium"));
        // The XCUITest driver requires that a path to the Carthage binary is in the PATH variable. I have this set for my shell, but the Java process does not see it. It can be inserted here.
        HashMap<String, String> environment = new HashMap();
        environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
        serviceBuilder.withEnvironment(environment);

        service = AppiumDriverLocalService.buildService(serviceBuilder);
        service.start();
    }
    return service;
}