1. 程式人生 > >Appium移動端自動化測試-指令碼啟動Appium服務端

Appium移動端自動化測試-指令碼啟動Appium服務端

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服務端");
			}
		}
		
	}

}