Java控制Appium server start/stop
相信很多人都会遇到这种场景,在进行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 添加
;C:\Program Files (x86)\Appium\node_modules\.bin;
然后在command line验证一下

如果出现这个,说明Appium使用默认参数启动成功
其实这个默认启动的是:C:\Program Files (x86)\Appium\node_modules\.bin\appium.bat

使用默认的参数,如果想使用指定的ip和port log等内容需要加参数
比如使用:
C:\Users\Test>appium -a 127.0.0.1 -p 1235
info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)
info: Appium REST http interface listener started on 127.0.0.1:1235
info: [debug] Non-default server args: {"address":"127.0.0.1","port":1235}
info: Console LogLevel: debug
具体参数这里不再详解,可以使用appium --help
那么好了,直接使用Java调用这个command的就好了
于是写了就这样:
public void excuteCMD(String comand)
{
Runtime rt = Runtime.getRuntime();
RuntimeExec rte = new RuntimeExec();
StreamWrapper error, output; try
{
Process proc = rt.exec(comand);
error = rte.getStreamWrapper(proc.getErrorStream(), "ERROR");
output = rte.getStreamWrapper(proc.getInputStream(), "OUTPUT");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s;
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
if (s.contains("Appium REST http"))
{
System.out.println("STARTED!");
}
} error.start();
output.start();
error.join(3000);
output.join(3000);
System.out.println("Output: " + output.message + "\nError: " + error.message);
} catch (IOException e)
{
e.printStackTrace();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
使用Java runtime class
传入相应的command, 然后执行并把输入显示出来
然后发现这样做行不通, 因为没法执行下面的code
查了相关资料才知道这个叫线程阻塞
于是乎只能找到一种非线程阻塞的方法
这个时候找到一个commons-exec的project能给解决问题
根据官方文档,如果使用非阻塞执行,可以这样做:
- 首先创建一个非阻塞的handler DefaultExecuteResultHandler,这个是专门用来处理非阻塞
- 在创建一个watchdog用来监控输出,设置timeout时间60s
- 创建一个执行器设置退出代码为1,代表执行成功
注意,这里必须设置一个waitfor time,如果没有设置会报错
resultHandler.waitFor(5000);
public static String APPIUMSERVERSTART = "C:\\Program Files (x86)\\Appium\\node_modules\\.bin\\appium.cmd";
public static void startServer() throws IOException, InterruptedException
{
startServer("4723");
// RuntimeExec appiumObj = new RuntimeExec();
// appiumObj.excuteCMD(APPIUMSERVERSTART);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
CommandLine commandLine = CommandLine.parse(APPIUMSERVERSTART);
ExecuteWatchdog dog = new ExecuteWatchdog(60 * 1000);
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.setWatchdog(dog);
executor.execute(commandLine, resultHandler);
resultHandler.waitFor(5000);
System.out.println("Appium server start");
}
以上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)
/**
* @author Young
* @param appiumServicePort
* @throws ExecuteException
* @throws IOException
*/
public static void stopAppiumServer(String appiumServicePort) throws ExecuteException, IOException
{
ExectorUtils.runWithWatchDog("cmd /c echo off & FOR /F \"usebackq tokens=5\" %a in"
+ " (`netstat -nao ^| findstr /R /C:\"" + appiumServicePort + "\"`) do (FOR /F \"usebackq\" %b in"
+ " (`TASKLIST /FI \"PID eq %a\" ^| findstr /I node.exe`) do taskkill /F /PID %a)");
}
这样就可以在每个test case启动相应的Appium server并且给出指定的参数。
相关资料:http://commons.apache.org/proper/commons-exec/tutorial.html
Java控制Appium server start/stop的更多相关文章
- 记录下通过Java代码打开cmd启动appium server及在使用过程中碰到的问题
1.appium server启动后,执行测试脚本,appium日志报错,提示appium setting未安装(原因是小米手机在用appium desktop调试时总是提示是否安装appium se ...
- Appium Server 传递Android参数
Appium server Capabilities 传递参数 Android 特定 Android Only Capability Description Values appActivit ...
- [Java聊天室server]实战之五 读写循环(服务端)
前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...
- Appium Server源码分析之作为Bootstrap客户端
Appium Server拥有两个主要的功能: 它是个http服务器,它专门接收从客户端通过基于http的REST协议发送过来的命令 他是bootstrap客户端:它接收到客户端的命令后,需要想办法把 ...
- Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决
Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决 I. 概述 1.1 JDBC概念 JDBC(Java Database Connectivity ...
- appium server日志分析
文章出处http://blog.csdn.net/yan1234abcd/article/details/60765295 每次运行测试,可以从Appium Server控制台看到有特别多的日志输出, ...
- Appium——详解Appium server capabilities
appium server capabilities来告诉appium,如何运行自动化测试,因此需要详细了解. 官方文档:http://appium.io/slate/en/master/?rub ...
- 移动端UI自动化Appium测试——Appium server两种启动方式
执行自动化测试之前,需要先运行appium server,这样才能形成server与java client的通信,启动server有两种方式,一种是命令,一种是按钮图标,具体使用如下: 1.用命令启动 ...
- Java NIO: Non-blocking Server 非阻塞网络服务器
本文翻译自 Jakob Jenkov 的 Java NIO: Non-blocking Server ,原文地址:http://tutorials.jenkov.com/java-nio/non-bl ...
随机推荐
- 关于FloatingActionButton
由于FloatingActionButton本质上是ImageView,跟ImageView相关的就不介绍,这里重点介绍新加的几个属性. app:fabSize:FloatingActionButto ...
- Tween公式 以及四个参数
Tween的主页在这里:http://createjs.com/tweenjs , 这里边还有挺多开源项目的: Tween公式 4个参数 t:current time(当前时间) b:beginnin ...
- SQL语句-创建索引
语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...
- git的基本介绍和使用
前言:从事iOS开发一年多以来,一直使用svn管理源代码.对svn的特点和弊端已经深有体会.前些天双十二前后,项目工期紧张到爆,起早贪黑的加班,可谓披星戴月,这还不止,回到家中还要疯狂的敲代码.那么问 ...
- 从点云到网格(二)VRIP介绍
VRIP(Volumetric Range Image Processing),顾名思义,是从深度图重建网格的一种方法.VRIP是Brian Curless和Marc Levoy在1996年提出来的方 ...
- 取消ie浏览器edge浏览器输入框右边的叉和眼睛
在ie高版本浏览器和edge浏览器里type为text和password的input框在输入时右边会出现×和眼睛,如果需要清除,方法如下: 首先在页面头部声明兼容性模式 <meta http-e ...
- maven log4g 用法
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> & ...
- selenium 页面截图并保存
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org. ...
- 高性能异步图片加载器的JS库:lazysizes
<script src="lazysizes.min.js" async=""></script> 使用示例 <!-- non-r ...
- TTTAttributedLabel xib sb lineSpacing not working
https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/733 set the same text in storyboard ...