在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java控制Appium server start/stop

2017-2-7 13:40| 发布者: zhangjf| 查看: 630| 评论: 0

摘要: 相信很多人都会遇到这种场景,在进行appium自动化的时候用Windows OS,不好实现后台运行,每次启动Appium server: 使用Appium GUI版手动点击 就是在cmd line 启动Appium 如果要实现CI,使用Appium GUI是不可行的 ...

相信很多人都会遇到这种场景,在进行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)Appiumnode_modules.bin;
复制代码

然后在command line验证一下

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

其实这个默认启动的是:C:Program Files (x86)Appiumnode_modules.binappium.bat

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

比如使用:

  1. C:UsersTest>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. try
  7. {
  8. Process proc = rt.exec(comand);
  9. error = rte.getStreamWrapper(proc.getErrorStream(), "ERROR");
  10. output = rte.getStreamWrapper(proc.getInputStream(), "OUTPUT");
  11. BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  12. String s;
  13. while ((s = stdInput.readLine()) != null)
  14. {
  15. System.out.println(s);
  16. if (s.contains("Appium REST http"))
  17. {
  18. System.out.println("STARTED!");
  19. }
  20. }
  21. error.start();
  22. output.start();
  23. error.join(3000);
  24. output.join(3000);
  25. System.out.println("Output: " + output.message + "nError: " + error.message);
  26. } catch (IOException e)
  27. {
  28. e.printStackTrace();
  29. } catch (InterruptedException e)
  30. {
  31. e.printStackTrace();
  32. }
  33. }
复制代码

使用Java runtime class

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

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

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

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

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

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

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

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

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

以上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

来自: http://www.cnblogs.com/tobecrazy/p/5118170.html

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 09:27

Copyright 2015-2025 djqfx

返回顶部