【问题标题】:How to stop and start Appium server programmatically using Java?如何使用 Java 以编程方式停止和启动 Appium 服务器?
【发布时间】:2016-04-17 04:00:57
【问题描述】:

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

【问题讨论】:

  • 参考我的博客链接 - happyautomation.blogspot.in/2015/01/…
  • 我引用了你的博客它显示错误----正在启动appium服务器--------Appium服务器启动成功! ---- 'C:/Program' 不是内部或外部命令、可运行程序或批处理文件。 ---- 正在停止 appium 服务器 ---- ---- Appium 服务器已成功停止! ---- 错误:找不到进程“node.exe”。
  • 保留您的 Appium 文件夹 D 或 E 盘或 C 盘。
  • @Shekhar Swami 仅安装在 C 盘上
  • 如果您将 appium 安装在 C:\Program Files (x86) 或 C:\Program Files ,那么 java 代码将抛出错误。将您的 Appium 设置目录剪切粘贴到根文件夹,因此路径将为C:\Appium\... 或 D:\Appium\.. 如果执行此操作,'C:/Program' 不被识别为内部或外部命令,则不会出现错误。如果您仍然遇到问题,请发布您的代码和错误

标签: java appium


【解决方案1】:

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

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

【讨论】:

  • 有时,service.stop() 无法停止执行。我们也可以从命令行中杀死特定端口对吗?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    上面提供的 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();
        }
    }
    

    【讨论】:

      【解决方案4】:

      请阅读这篇文章——它将解释如何开始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);
      

      【讨论】:

      【解决方案5】:

      试试这个 - 它对我有用

      示例代码:

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 2018-10-07
      • 1970-01-01
      • 2021-04-13
      • 2019-07-15
      • 2019-01-14
      • 2018-11-04
      • 2018-10-14
      相关资源
      最近更新 更多