Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java以编程方式停止和启动Appium服务器?_Java_Appium - Fatal编程技术网

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

如何使用Java以编程方式停止和启动Appium服务器?,java,appium,Java,Appium,如何使用Java代码启动和停止服务器?目前我正在手动执行此过程。请阅读此内容-它将解释如何启动 最新的java客户端有api来实现这一点 这是一种编程启动的方式,页面列出了更多这样的方式 AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); final DesiredCapabilities capabilities = new DesiredCapab

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

请阅读此内容-它将解释如何启动

最新的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);
AppiumDriverLocalService=AppiumDriverLocalService.buildDefaultService();
最终DesiredCapabilities=新DesiredCapabilities();
//能力.setCapability(CapabilityType.BROWSER_名称,“浏览器”);
能力。设置能力(“deviceName”、“MiPad”);
能力。设置能力(“平台名”、“安卓”);
能力。设置能力(“automationName”、“Appium”);
//能力。设置能力(“avd”、“firstavd”);
能力。设置能力(“appPackage”、“com”);
capabilities.setCapability(“appActivity”、“.ui.activity”);
驱动程序=新的AndroidDriver(服务、功能);

对于阅读本文的任何人,如果他们碰巧正在使用npm(node/js/typescript),我已经创建了一个名为的模块,可以在后台以编程方式(mac或windows)启动和停止appium。

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

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/>
public void startServer(){
Runtime=Runtime.getRuntime();
试一试{
runtime.exec(“cmd.exe/c start cmd.exe/k\”appium-a 127.0.0.1-p 4723——会话覆盖-dc\”{\“noReset\”:\“false\”}\”);
睡眠(10000);
}捕获(IOException | InterruptedException e){
e、 printStackTrace();
}
}
公共void stopServer(){
Runtime=Runtime.getRuntime();
试一试{
runtime.exec(“taskkill/F/IM node.exe”);
runtime.exec(“taskkill/F/IM cmd.exe”);
}捕获(IOE异常){
e、 printStackTrace();
}
}
我觉得这很有帮助,希望有帮助。
来源:

试试这个-它对我有用

示例代码:

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;
}
public静态AppiumDriverLocalService startAppiumServer(){
布尔标志=checkifserverisrunning(4723);
如果(!标志)
{
AppiumServiceBuilder serviceBuilder=新的AppiumServiceBuilder();
//使用任何端口,以防已使用默认的4723(可能由另一台Appium服务器使用)
usingAnyFreePort();
//告诉serviceBuilder节点的安装位置。或者在名为node\u path的环境变量中设置此路径
serviceBuilder.usingDriverExecutable(新文件(“/Users/Puneetha/.nvm/versions/node/v11.10.1/bin/node”);
//告诉serviceBuilder Appium的安装位置。或者在名为Appium\u path的环境变量中设置此路径
serviceBuilder.withAppiumJS(新文件(“/Users/Puneetha/.nvm/versions/node/v11.10.1/bin/appium”);
//XCUITest驱动程序要求path变量中包含迦太基二进制文件的路径。我为shell设置了该路径,但Java进程没有看到它。可以将其插入此处。
HashMap环境=新建HashMap();
environment.put(“PATH”),“/usr/local/bin:”+System.getenv(“PATH”);
serviceBuilder.withEnvironment(环境);
service=AppiumDriverLocalService.buildService(serviceBuilder);
service.start();
}
回程服务;
}

顶部提供的AppiumDriverLocalService解决方案解决了我在手动启动服务器时不断收到的套接字挂起问题

这是我的代码中的外观:

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();
    }
}
公共最终类AppiumServiceProvider{
私有最终静态记录器log=Logger.getLogger(AppiumServiceProvider.class);
私有静态AppiumDriverLocalService=null;
私有静态最终测试配置TestConfiguration=newtestconfiguration();
私有AppiumServiceProvider(){
}
公共静态AppiumDriverLocalService getService(){
if(服务==null){
initAppiumService();
}
if(服务==null){
失败(“无法启动Appium驱动程序本地服务”);
}
回程服务;
}
私有静态void initAppiumService(){
Map env=newhashmap(System.getenv());
环境放置(“路径”),/usr/local/bin:“+env.get(“路径”);
env.put(“ANDROID_HOME”,“/Users/nets/Library/ANDROID/sdk”);
service=AppiumDriverLocalService.buildService(新的AppiumServiceBuilder()
.usingAnyFreePort()
.withAppiumJS(新文件(testConfiguration.getAppiumJsLocation()))
.usingDriverExecutable(新文件(“C:\\Program Files\\nodejs\\node.exe”))
.withArgument(GeneralServerFlag.SESSION\u覆盖)
.withArgument(GeneralServerFlag.LOG_级别,“错误”)
.withArgument(GeneralServerFlag.Released_安全性)
.withEnvironment(环境)
.withLogFile(新文件(“target/appium.log”))
.具有启动超时(60,时间单位为秒);
log.info(“新应用程序服务:+service.getUrl());
service.start();
}
}

参考我的博客链接-我参考了你的博客,它显示错误-----启动appium服务器-----appium服务器已成功启动C:/Program'未被识别为内部或外部命令、可操作程序或批处理文件。---正在停止appium服务器-----appium服务器已成功停止!-错误:找不到进程“node.exe”。请保留您的Appium文件夹D或E驱动器或C驱动器。@Shekhar Swami仅安装在C驱动器上如果您在C:\Program Files(x86)或C:\Program Files安装Appium,则java代码将抛出错误。请剪切粘贴根文件夹中的Appium设置目录,使路径为C:\Appium\。。。或D:\Appium\。。如果执行此操作,“C:/Program”将不会重新记录