相信很多人都会遇到这种场景,在进行appium自动化的时候用Windows OS,不好实现后台运行,每次启动Appium server:

  • 使用Appium GUI版手动点击
  • 就是在cmd line 启动Appium

如果要实现CI,使用Appium GUI是不可行的,因为如果在跑case的过程中Appium session无法创建必须重启Appium server,也无法自动获取相应的参数直接启动Appium

那么这个时候只能使用command line

PS:使用command line需要把Appium相关加入到环境变量

在path 添加

  1. ;C:\Program Files (x86)\Appium\node_modules\.bin;

然后在command line验证一下

如果出现这个,说明Appium使用默认参数启动成功

其实这个默认启动的是:C:\Program Files (x86)\Appium\node_modules\.bin\appium.bat

使用默认的参数,如果想使用指定的ip和port log等内容需要加参数

比如使用:

  1. C:\Users\Test>appium -a 127.0.0.1 -p 1235
  2. info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)
  3. info: Appium REST http interface listener started on 127.0.0.1:1235
  4. info: [debug] Non-default server args: {"address":"127.0.0.1","port":1235}
  5. info: Console LogLevel: debug

具体参数这里不再详解,可以使用appium --help


那么好了,直接使用Java调用这个command的就好了

于是写了就这样:

  1. public void excuteCMD(String comand)
  2. {
  3. Runtime rt = Runtime.getRuntime();
  4. RuntimeExec rte = new RuntimeExec();
  5. StreamWrapper error, output;
  6.  
  7. try
  8. {
  9. Process proc = rt.exec(comand);
  10. error = rte.getStreamWrapper(proc.getErrorStream(), "ERROR");
  11. output = rte.getStreamWrapper(proc.getInputStream(), "OUTPUT");
  12. BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  13. String s;
  14. while ((s = stdInput.readLine()) != null)
  15. {
  16. System.out.println(s);
  17. if (s.contains("Appium REST http"))
  18. {
  19. System.out.println("STARTED!");
  20. }
  21. }
  22.  
  23. error.start();
  24. output.start();
  25. error.join(3000);
  26. output.join(3000);
  27. System.out.println("Output: " + output.message + "\nError: " + error.message);
  28. } catch (IOException e)
  29. {
  30. e.printStackTrace();
  31. } catch (InterruptedException e)
  32. {
  33. e.printStackTrace();
  34. }
  35. }

使用Java runtime class

传入相应的command, 然后执行并把输入显示出来

然后发现这样做行不通, 因为没法执行下面的code

查了相关资料才知道这个叫线程阻塞

于是乎只能找到一种非线程阻塞的方法


这个时候找到一个commons-exec的project能给解决问题

根据官方文档,如果使用非阻塞执行,可以这样做:

  • 首先创建一个非阻塞的handler DefaultExecuteResultHandler,这个是专门用来处理非阻塞
  • 在创建一个watchdog用来监控输出,设置timeout时间60s
  • 创建一个执行器设置退出代码为1,代表执行成功

注意,这里必须设置一个waitfor time,如果没有设置会报错

  1. resultHandler.waitFor(5000);
  1. public static String APPIUMSERVERSTART = "C:\\Program Files (x86)\\Appium\\node_modules\\.bin\\appium.cmd";
  2.  
  3. public static void startServer() throws IOException, InterruptedException
  4. {
  5.  
  6. startServer("4723");
  7. // RuntimeExec appiumObj = new RuntimeExec();
  8. // appiumObj.excuteCMD(APPIUMSERVERSTART);
  9. DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  10. CommandLine commandLine = CommandLine.parse(APPIUMSERVERSTART);
  11. ExecuteWatchdog dog = new ExecuteWatchdog(60 * 1000);
  12. Executor executor = new DefaultExecutor();
  13. executor.setExitValue(1);
  14. executor.setWatchdog(dog);
  15. executor.execute(commandLine, resultHandler);
  16. resultHandler.waitFor(5000);
  17. System.out.println("Appium server start");
  18. }

以上code实现的是使用默认的port启动Appium server ,如果遇到Appium端口被占用,启动失败怎么办?

我的策略是先杀掉所以占用这个端口的进程:

可以尝试使用command line

cmd /c echo off & FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"4723"`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I node.exe`) do taskkill /F /PID %a)

  1. /**
  2. * @author Young
  3. * @param appiumServicePort
  4. * @throws ExecuteException
  5. * @throws IOException
  6. */
  7. public static void stopAppiumServer(String appiumServicePort) throws ExecuteException, IOException
  8. {
  9. ExectorUtils.runWithWatchDog("cmd /c echo off & FOR /F \"usebackq tokens=5\" %a in"
  10. + " (`netstat -nao ^| findstr /R /C:\"" + appiumServicePort + "\"`) do (FOR /F \"usebackq\" %b in"
  11. + " (`TASKLIST /FI \"PID eq %a\" ^| findstr /I node.exe`) do taskkill /F /PID %a)");
  12. }

这样就可以在每个test case启动相应的Appium server并且给出指定的参数。


相关资料:http://commons.apache.org/proper/commons-exec/tutorial.html

Java控制Appium server start/stop的更多相关文章

  1. 记录下通过Java代码打开cmd启动appium server及在使用过程中碰到的问题

    1.appium server启动后,执行测试脚本,appium日志报错,提示appium setting未安装(原因是小米手机在用appium desktop调试时总是提示是否安装appium se ...

  2. Appium Server 传递Android参数

    Appium  server Capabilities 传递参数    Android 特定 Android Only Capability Description Values appActivit ...

  3. [Java聊天室server]实战之五 读写循环(服务端)

    前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...

  4. Appium Server源码分析之作为Bootstrap客户端

    Appium Server拥有两个主要的功能: 它是个http服务器,它专门接收从客户端通过基于http的REST协议发送过来的命令 他是bootstrap客户端:它接收到客户端的命令后,需要想办法把 ...

  5. Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决

    Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决 I. 概述 1.1 JDBC概念 JDBC(Java Database Connectivity ...

  6. appium server日志分析

    文章出处http://blog.csdn.net/yan1234abcd/article/details/60765295 每次运行测试,可以从Appium Server控制台看到有特别多的日志输出, ...

  7. Appium——详解Appium server capabilities

      appium server capabilities来告诉appium,如何运行自动化测试,因此需要详细了解. 官方文档:http://appium.io/slate/en/master/?rub ...

  8. 移动端UI自动化Appium测试——Appium server两种启动方式

    执行自动化测试之前,需要先运行appium server,这样才能形成server与java client的通信,启动server有两种方式,一种是命令,一种是按钮图标,具体使用如下: 1.用命令启动 ...

  9. Java NIO: Non-blocking Server 非阻塞网络服务器

    本文翻译自 Jakob Jenkov 的 Java NIO: Non-blocking Server ,原文地址:http://tutorials.jenkov.com/java-nio/non-bl ...

随机推荐

  1. iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

    iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43      阅读:630      评论:0      收藏:0   ...

  2. [转] JAVA读取excel数据(插入oracle数据库)

    原文地址:http://blog.csdn.net/zczzsq/article/details/16803349 本实例做的是读取execl(只能读取.xls的execl,即只能读取03版的),如果 ...

  3. 使用SharpZipLib实现文件压缩、解压

    接口 public interface IUnZip { /// <summary> /// 功能:解压zip格式的文件. /// </summary> /// <par ...

  4. git提示:Fatal:could not fetch refs from ....

    在git服务器上新建项目提示: Fatal:could not fetch refs from git..... 百度搜索毫无头绪,最后FQgoogle,找到这篇文章http://www.voidcn ...

  5. PRINCE2特征(二)

    英国体系环境下项目有什么特征(二) 今天又要和大家分享了,这个时间也是自己很喜欢的时刻.上次给大家分享的是英国体系下项目的特征之一:临时性.不知道大家还有没有印象,英国体系下项目的特征有五个,今天来给 ...

  6. .net Socket 通信简单实例(初级入门)

    c/s控制台应用程序,Server.Client分别在两个项目中 服务端 using System; using System.Collections.Generic; using System.Li ...

  7. jquery tree

      <div class="fl left"  id="nav" style="width:18%" >             ...

  8. centos 6.4 getmail 收取163 邮件

    #CentOS 6.6 64bit 默认yum 源没有getmail rpm包#首先安装EPEL yum 源EPEL(Extra Packages for Enterprise Linux):http ...

  9. cpp 调用python

    在用cpp调用python时, 出现致命错误: no module named site  ,  原因解释器在搜索路径下没有找到python库.可以在调用Py_Initialize前,调用 Py_Se ...

  10. Download Excel file with Angular

    源码连接(编写中) 用Angular下载后台返回的Excel文件,用Blob实现,引用FileSaver.js 后台C#代码: [WebMethod] public static byte[] Cal ...