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);
} catch (IOException 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 = "start 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服務端");
}
}
}
}