【问题标题】:Start appium server programmatically in Windows machine在 Windows 机器上以编程方式启动 appium 服务器
【发布时间】:2017-10-22 23:07:54
【问题描述】:

到目前为止,我使用的是 Windows 8.1 和 appium 1.4.16,我想升级 appium,所以我只是从控制面板中卸载了 1.4.16,然后在安装最新的 appium 之后安装了 node.js

npm install -g appium

当我跑步时

appium -v

它显示我 1.6.4 到现在没有问题。 之后在我的 Maven 项目中,我想以编程方式启动 appium 服务器,但 appium 没有保存在

C:/Program Files or C:/ProgramFile(x86).

如何以编程方式启动 appium 服务器?

我正在使用下面的代码来运行 appium

Process p = Runtime.getRuntime().exec("\"C:/Program Files/Appium/node.exe\" \"C:/Program Files/Appium/node_modules/appium/bin/Appium.js\" --full-reset --local-timezone");

【问题讨论】:

    标签: selenium-webdriver appium


    【解决方案1】:

    你可以试试下面的代码:

    AppiumServiceBuilder builder = new AppiumServiceBuilder()
                .withAppiumJS(new File("C:\Users\<Username>\node_modules\appium\build\lib\main.js"))
                .withArgument(GeneralServerFlag.APP,  path of your app );
    appiumDriverLocalService = builder.build();
    appiumDriverLocalService.start();
    

    【讨论】:

    • 有没有其他方法可以将appium安装到C:/ProgramFiles
    • no..我们正在通过 npm 正确安装..所以它只会安装在 node_modules 文件夹中
    【解决方案2】:

    如果它是通过 npm 安装的,那么 Appium 可以以编程方式运行。 Appium 的安装位置无关紧要。下面的代码模仿了我们从命令提示符打开它的手动行为。

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

    还有其他几种方法,具体取决于您安装 Appium 的方式。你可以在这里查看这些 - Start Appium Server Programatically

    【讨论】:

      【解决方案3】:
      String nodePath = "C:/Appium/node.exe";
       // Set path of your appium.js file.
       String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";
       String cmd = nodePath + " " + appiumJSPath;
       AndroidDriver driver;
      
       // This method Is responsible for starting appium server.
       public void appiumStart() throws IOException, InterruptedException {
        // Execute command string to start appium server.
        p = Runtime.getRuntime().exec(cmd);
        // Provide wait time of 10 mins to start appium server properly.
        // If face any error(Could not start a new session...) then Increase
        // this time to 15 or 20 mins.
        Thread.sleep(10000);
        if (p != null) {
         System.out.println("Appium server Is started now.");
        }
       }
      
       // This method Is responsible for stopping appium server.
       public void appiumStop() throws IOException {
        if (p != null) {
         p.destroy();
        }
        System.out.println("Appium server Is stopped now.");
       }
      

      【讨论】:

        【解决方案4】:

        有 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)在 Node.exe 中使用 Appium.js

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


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

        【讨论】:

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