【问题标题】:Run the appium server programmatically以编程方式运行 appium 服务器
【发布时间】:2019-08-07 05:15:14
【问题描述】:

我正在尝试以编程方式运行 Appium 服务器。我尝试了 Appium 提供的所有可能选项,但没有得到任何结果

从命令行,如果我正在尝试C:\Users\User>appium,它正在启动服务器。如何使用 java 代码来做同样的事情?

注意:Appium的版本是1.6.5

AppiumDriverLocalService service=AppiumDriverLocalService.buildDefaultService(); 
service.start(); 

这是我用来以编程方式运行 appium 服务器的代码。

我得到的错误是

java.lang.NoClassDefFoundError: org/apache/commons/validator/routines/InetAddressValidator

【问题讨论】:

  • 以编程方式执行时遇到什么错误?那里使用的代码是什么?
  • AppiumDriverLocalService 服务=AppiumDriverLocalService.buildDefaultService();服务.start();这是我用来以编程方式运行 appium 服务器的代码。我得到的错误是 java.lang.NoClassDefFoundError: org/apache/commons/validator/routines/InetAddressValidator
  • 请更新问题,在 cmets 中提供更多详细信息可能会丢失完整性。
  • NoClassDefFoundError 是你编译或运行代码错误的标志
  • 可以参考这里的答案:stackoverflow.com/a/30440985/1872682

标签: java appium


【解决方案1】:

你试过了吗,

import java.io.*;

public class CmdTest {
    public static void main(String[] args) throws Exception {

        Process p = Runtime.getRuntime().exec("appium");

    }
}

【讨论】:

  • 是的,我尝试过,但出现 java.io.IOException 之类的错误:无法运行程序“C:/Users/User>appium”:CreateProcess 错误=2,系统找不到在 java.io 中指定的文件。 lang.ProcessBuilder.start(未知来源)
  • 它明确表示appium不在此路径下,尝试更改路径,安装时appium所在的位置。
  • 但是,如果我从命令行尝试相同的操作,我怎么能运行服务器
  • 在命令行中,所有用户身份验证都可以正常使用,但在编程上,身份验证在更多方面缺乏。
  • 1) Windows 需要转义反斜杠。 2) C:\Users\User> 不应该在那里
【解决方案2】:

我知道您要求使用 Java,但我使用 C# 以编程方式执行此操作。因此,我粘贴我的解决方案以防万一:

C# 代码:

    public void startAppiumServerLocally()
    {
        try
        {
            string nodejs = "\"" + Environment.GetEnvironmentVariable("nodejs") + "\""; // Environment path for node.exe
            string appium = "\"" + Environment.GetEnvironmentVariable("appium") + "\""; // Environment path for main.js or appium.js

            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(nodejs);

            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.RedirectStandardError = true;
            myProcessStartInfo.CreateNoWindow = true;
            // getOverride() method returns '--session-override'
            // getDesiredCapabilitiesJson() returns a JSON with some capabilities '--default-capabilities {"udid":identifier..., deviceName: "Samsung S7"....}'
            // getDriverPort() returns --port X just in case I need to start in a different and unused port.
            string args = appium + getOverride() + getDesiredCapabilitiesJson() + getDriverPort();
            myProcessStartInfo.Arguments = args;

            nodeProcess = new Process();
            nodeProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            nodeProcess.StartInfo = myProcessStartInfo;
            nodeProcess.Start();

            nodeProcess.BeginErrorReadLine();
            StreamReader myStreamReader = nodeProcess.StandardOutput;
            while (_bAppiumForceEnd == false)
            {
                if (_bAppiumInit == false)
                {
                    string line = myStreamReader.ReadLine();
                    if (line.Contains("listener started"))
                    {
                        // _bAppiumInit is a flag to start my tests once the server started
                        _bAppiumInit = true;
                    }
                }
                else
                {
                    myStreamReader.ReadLine();
                }
            }
            myStreamReader.Close();
        }
        catch (Exception e)
        {
            //Log(e.Message);
        }
    }

我知道创建一个while循环不是最好的解决方案,但有时我的nodeProcess并没有停止......所以我将它改为这种方式,它可以完美运行。

【讨论】:

    【解决方案3】:

    确保您在全局范围内安装了 Appium...

    npm install -g appium

    【讨论】:

      【解决方案4】:

      以下是使用 Java 代码以编程方式启动和停止 appium 服务器的方法

      import java.nio.file.Paths;    
      public class AppiumSetupAndTearDown {
      
              public static void startAppiumServer() throws IOException, InterruptedException {
      
                  String logDir = <the path where you want to create the appium output file>;
      
                  File appiumOutput = new File(logDir);
      
                  if (!appiumOutput.exists()) {
                      boolean status = appiumOutput.createNewFile();
                      if (!status) {
                          throw new IOException("Failed to create Appium output file!");
                      }
                  }
      
                  String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/startAppium.sh"};
                  ProcessBuilder pb = new ProcessBuilder(cmd);
      
                  pb.redirectError(new File(logDir));
                  pb.redirectInput(new File(logDir));
                  pb.redirectOutput(new File(logDir));
                  pb.start();
                  System.out.println("Appium server started!");
      
              }
      
              public static void stopAppiumServer() throws IOException, InterruptedException {
                  String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/stopAppium.sh"};
      
                  ProcessBuilder pb = new ProcessBuilder(cmd);
                  pb.start();
                  System.out.println("Appium server stopped!");
      
              }
          }
      

      您也可以在启动和停止 appium 服务器后添加 Thread.sleep(5000),以防遇到时间问题。

      【讨论】:

        【解决方案5】:

        尝试将公共验证器库添加到您的类路径/pom.xml (commons-validator-1.4.0.jar)

        【讨论】:

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