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 ...
随机推荐
- RedisUtil 工具类
package com.test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import red ...
- 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送
原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...
- [转]如何设置eclipse中js默认打开为java Editor
打开window-preference -> General-Editors-File Associator 看到右边的.js下边就是设置默认打开方式了 转自百度知道:http://zhidao ...
- 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件
演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...
- oracle数据查询
select * from XMBL_EM_DBBACK where f_djbh='DB01201612270013'select * from flow_task_list where biz_d ...
- java面向对象---对象容器
泛型类--ArrayList<>; 2.对象数组中的每个元素都是对象的管理者而并非对象本身!!!!! 3.java类的基本数据类型 基本数据类型 包装类 byte Byte short S ...
- 【poj2154】 Color
http://poj.org/problem?id=2154 (题目链接) 题意 n个珠子的项链,可以染上n中颜色,项链可以旋转不能翻转,求染色方案数. Solution 经典的公式: \begin{ ...
- .net Socket 通信简单实例(初级入门)
c/s控制台应用程序,Server.Client分别在两个项目中 服务端 using System; using System.Collections.Generic; using System.Li ...
- Set集合
Set:无序(存储和取出数据不一致):唯一性(不可重复) Set是Collection的子类,所以Collection的方法Set都可以使用 HashSet:底层为哈希表,保证唯一性,依赖hashCo ...
- 逆向工程学习第二天--动手开发自己的第一个shellcode
一个简单的c语言添加windows管理员账号的小程序,之前在渗透的时候经常用到,现在拿它来做自己的第一个shellcode. C代码: #pragma comment(lib, "netap ...