【Appnium 小试牛刀之二】java实现根据不同端口号,多线程启动appnium server

package Common;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class AppiumServerController {
	
	public Map<String,Object> map = new HashMap<String,Object>();	
    private HashMap<String, Process> processHashMap = new HashMap<String, Process>();
	String node;
	String appium_js;
	String port;
	String bootstrap_port;
	String chromedriver_port=;

    public void startServer(String nodePath, String appiumPath, String host, String port, String bootstrapPort, String chromeDriverPort, String udid) throws Exception {
        Process process;
        BufferedReader input_reader = null;
        BufferedReader err_reader = null;
        String cmd = nodePath +" "+ appiumPath + " -a \"" + host + "\" --session-override " + " -p \"" 
                + port + "\" -bp \"" + bootstrapPort + "\" --chromedriver-port \"" + chromeDriverPort + "\" -U \"" 
                + udid + "\"";
        System.out.println("当前执行的appnium的启动命令为:"+cmd);
        process = Commons.excuteShell(cmd);
        if(process != null){
            processHashMap.put(port, process); 
            input_reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            err_reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    		String line = null;
    		String lines = null;
            while ((line = input_reader.readLine()) != null || (lines = err_reader.readLine()) != null) {
            	if(line != null){
        			if (line.indexOf("Appium REST http interface listener started on") != -1) {
        				System.out.println("STRAT appium server:" + port);
        				map.put(port, true);     
                    }
    				System.out.println("appnium input log :" + line);
            	}
            	if(lines != null){
            		if (lines.indexOf("Couldn't start Appium REST http interface listener") != -1) {
            			System.out.println("Stop appium server:" + port);
                		map.put(port, false);
                    }
            		System.out.println("appnium error log :" + lines);
            	}
            } 
        }
        process.waitFor();
    }

    public boolean stopServer(Process process) {
    	boolean flag = false;
    	try{
	        if (process != null) {
	            process.destroy();
	            flag = true; 
	        }
    	}catch (Exception e) {
    		System.err.println("当前stopServer接口 错误原因为:" + e.getMessage());
    		flag = false;
    	}
		return flag;
    }

    public boolean stopServer(String port) {
    	boolean flag = false;
    	try{
	        Process process = processHashMap.get(port);
	        stopServer(process);
	        processHashMap.remove(port);
	        flag = true;
    	}catch (Exception e) {
    		System.err.println("当前stopServer接口:" + port + " 错误原因为:" + e.getMessage());
    		flag = false;
    	}
    	
		return flag;
    }
	
	
	
	@Override
	public void run() {
    	try {
			Thread.sleep(2000);
		} catch (Exception e) {
			System.err.println("当前run错误原因为:" + e.getMessage());
		}		
	}
	
	public void appnium_starts(String UIDs, String host, int ports , int bootstrap_ports, int chromedriver_ports) throws Exception {
		try {
			int k = 0;
			System.out.println("当前启动appnium端口为:" + ports);
			list.add(k++, String.valueOf(ports));
			startServer(node, appium_js, host, String.valueOf(ports),
					String.valueOf(bootstrap_ports),
					String.valueOf(chromedriver_ports), UIDs);
		} catch (Exception e) {
			System.err.println("IOException" + e.getMessage());
		}
	}
	
	public void appnium_start(final int threadNumber, List<String> UIDs, List<String> hosts) throws Exception {
	    for (int i= 0; i < threadNumber; i++) {
			final String UID = UIDs.get(i);
			final String host = hosts.get(i);
			final int ports = Integer.parseInt(port) + i;
			final int bootstrap_ports = Integer.parseInt(bootstrap_port) + i;
			final int chromedriver_ports = Integer.parseInt(chromedriver_port) + i;
	        new Thread(new Runnable() {
	            @Override
	            public void run() {
	            	try {
						//int count = (int) Thread.currentThread().getId();
						appnium_starts(UID, host, ports, bootstrap_ports, chromedriver_ports);
						//Thread.sleep(2000);
					} catch (Exception e) {
						System.err.println("当前startThreads错误原因为:" + e.getMessage());
					}
	              // System.out.println("当前线程:" + Thread.currentThread().getId());
	            }
	        }).start();
	    }
	}
	
	public void main(String[] args) throws Exception {
	port=4723
	bootstrap_port=4823
	chromedriver_port=8923
	node="node";//没有配置环境变量,请配置node路径
	appium_js="E:\\AppiumForWindows-1.3.4.1\\Appium\\node_modules\\appium\\lib\\server\\main.js";
		List<String> hosts = new ArrayList<String>();
		List<String> UID = new ArrayList<String>();
		UID.add("aaaaaaaaa");
		UID.add("bbbbbbbbb");
		hosts.add("127.0.0.1");
		hosts.add("127.0.0.1");
		appnium_start(2, UID, hosts);
		Thread.sleep(1000000);
        appnium_stop();	
	}

}
package Common;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;


public class Commons {

	//region app 执行shell命令
	/**
	 * 执行adb命令
	 * 
	 * @param s 要执行的命令  参数
	 * 
	 */
	public static Process excuteShell(String s) {
		Process proc = null;
		Runtime runtime = Runtime.getRuntime();
		try {
			proc = runtime.exec(s);
		} catch (Exception e) {
			System.out.print("执行命令:" + s + "出错啦!");
			return null;
		}
		return proc;
	}
	//endregion
	
}

思路:采用cmd运行node命令,来启动appnium命令,更多命令参数,可以看main.js帮助文档

启动命令案例:

node E:\AppiumForWindows-1.3.4.1\Appium\node_modules\appium\lib\server\main.js --address "127.0.0.1" --session-override -p "5545"  -bp "5555" --chromedriver-port "5565" --pre-launch  --automation-name "Appium" --platform-name "Android" --platform-version "5.1" --app-pkg "com.tencent.mobileqq"  --app-activity ".activity.SplashActivitivity" --device-name "W4OZLZ6L4TWG4HCU"  -U "W4OZLZ6L4TWG4HCU"

注意:

1、node没有配置环境变量,可以直接用node.exe路径代替

2、以上只是一个小案例,没有引入adb获取设备信息,以及连接appnium server和启动手机客户端,请关注后续会博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值