1:脚本启动Appium服务端的步骤
1.1:执行DOS命令的封装方法
package com.AutoAPP.Utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 执行DOS命令
*/
public class DosCommand {
private static Process process;
public static void RunDosCommand(String DOSCommand) {
try {
Runtime.getRuntime().exec("cmd /c " + DOSCommand);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @throws Exception
* @override RunDosCommand
*
* */
public static List<String> RunDosCommand(String DOSCommand, boolean isgetInfo) throws Exception {
List<String> context = null;
try {
process = Runtime.getRuntime().exec("cmd /c " + DOSCommand);
Thread.sleep(10000);//注意 这里一定要延时 不然启动不了Appium
} catch (Exception e) {
e.printStackTrace();
}
if(isgetInfo){
context = new ArrayList<>();
InputStream in = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine())!=null){
context.add(line);
}
process.waitFor();
process.destroy();
}
return context;
}
}
1.2:获取已连接的手机的udid和数量并判断手机连接的状态
只有Device状态的手机才返回有效的udid值
package com.AutoAPP.Utils;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;
/**
* 获取连接上的手机的信息
* */
public class ConnectedInfo {
/**
* 获取连接上的手机数量
* */
@Test
public int getConnectedNum(){
int DeviceNumbers = 0;
try {
List<String> runDosCommand = DosCommand.RunDosCommand("adb devices", true);
DeviceNumbers = runDosCommand.size() -2;
} catch (Exception e) {
e.printStackTrace();
}
return DeviceNumbers;
}
/**
* 获取uuid
* */
public List<String> getUUID(){
List<String> runDosCommand = null;
List<String> UdidList = new ArrayList<>();
try {
runDosCommand = DosCommand.RunDosCommand("adb devices", true);
} catch (Exception e) {
e.printStackTrace();
}
if(runDosCommand.size() <= 2){
System.err.println("当前没有设备连接上Appium服务端");
}else{
for(int i = 0 ; i < (runDosCommand.size()-1) ; i++){
String uuid = runDosCommand.get(i).split("\t")[0];
String status = runDosCommand.get(i).split("\t")[1];
if(status.equalsIgnoreCase("device")){
UdidList.add(uuid);
}else{
System.err.println("设备:"+uuid+" 连接状态异常,请排查该设备连接状态");
}
}
}
return UdidList;
}
}
1.3:根据连接的手机数量随机生成端口
根据手机连接上的数量产生随机的
package com.AutoAPP.Utils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.testng.annotations.Test;
/**
* 根据连接上的手机数量生成port 并且要判断port不是冲突的 如果冲突 则重新生成
* */
public class RandomPort {
@Test
public static List<Integer> Ports(){
Set<Integer> Ports = new HashSet<>();
List<Integer> PortList = new ArrayList<>();
int connectedNum = ConnectedInfo.getUUID().size();
//生成随机数
//int intFlag = (int)(Math.random() * 10000);
while(true){
List<String> runDosCommand = null;
int port = (int)((Math.random()*9+1)*1000);
try {
runDosCommand = DosCommand.RunDosCommand("netstat -ano|findstr "+port, true);
} catch (Exception e) {
e.printStackTrace();
}
if(runDosCommand.size() == 0){
Ports.add(port);
}else{
System.err.println("端口:"+port+"被占用");
}
if(Ports.size() >= (2*connectedNum)){
break;
}
}
for(int port : Ports){
PortList.add(port);
}
return PortList;
}
public static void main(String[] args) {
List<Integer> ports = Ports();
System.out.println(ports.toString());
}
}
1.4:生成端口 每个手机两个端口 组装命令
1:执行命令前需要将node.exe的进程全部杀死
2:命令如下:appium -p 5000-bp 6000-U udid
-p 端口号表示脚本与服务端连接的端口
-bp 表示服务端与设备进行通信的端口
-U 表示针对某台设备启动的服务,值为设备的udid
3:最终执行的脚本
appium -p 5000-bp 6000-U udid >路径\udid.log
package com.AutoAPP.Utils;
import java.util.List;
import org.testng.annotations.Test;
/**
* 启动Appium服务 appium -p 5000-bp 6000-U udid >路径\\udid.log
* */
public class StartAppium {
@Test
public void initAppium(){
DosCommand.RunDosCommand("taskkill /f /t /im node.exe");
// DosCommand.RunDosCommand("appium");
}
@Test
public void startAppium(){
this.initAppium();
String path = System.getProperty("user.dir");
List<String> uuidList = ConnectedInfo.getUUID();
List<Integer> ports = RandomPort.Ports();
for(int i = 1 ;i <= uuidList.size() ; i++){
if(ports.size() !=1){
String DOSCommand = "appium -p "+ports.get(i-1)+" -bp "+ports.get((ports.size())-i)+" -U "+uuidList.get(i-1)+" >"+path+"\\"+uuidList.get(i-1)+".log";
DosCommand.RunDosCommand(DOSCommand);
System.out.println(DOSCommand);
}else{
System.err.println("当前没有设备连接上Appium服务端");
}
}
}
}