【问题标题】:Starting appium server through Java通过 Java 启动 appium 服务器
【发布时间】:2017-05-18 20:58:35
【问题描述】:

我正在使用 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 中调用时,出现错误“appium.sh:Error on line1 - appium command not found”

当我通过终端调用相同的shell脚本时,appium服务器启动成功。

【问题讨论】:

    标签: java macos shell terminal appium


    【解决方案1】:

    您可以使用下面的代码使用java代码启动appium服务器并在初始化appium时使用service_url 驱动程序。示例取自THIS 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();
    
        }
    }
    

    【讨论】:

      【解决方案2】:
          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

      【讨论】:

        【解决方案3】:

        如果您有兴趣了解它应该如何实现,请查看 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-17
          • 1970-01-01
          • 2019-06-19
          • 1970-01-01
          • 2021-05-20
          • 1970-01-01
          • 2021-04-13
          相关资源
          最近更新 更多