通过代码启动appium服务

如果选择npm安装的最新版appium 1.8.0以上版本,启动appium的时候,你会发现无法使用Node.js命令

这里给出两种方法来启动appim:

方法一:

/**
* 使用AppiumServiceBuilder+AppiumDriverLocalService 开启appium服务(可以添加appium参数)
*/
public void startAppiumServer() {
//Set Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
cap.setCapability("noSign", "true");
cap.setCapability("unicodeKeyboard", true);
cap.setCapability("resetKeyboard", true); //Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error"); //Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
} public void stopAppiumServer() {
service.stop();
Log.logInfo("Appium stopped!!");
} public boolean checkIfServerIsRunnning(int port) { boolean isServerRunning = false;
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
serverSocket.close();
} catch (IOException e) {
//If control comes here, then it means that the port is in use
isServerRunning = true;
} finally {
serverSocket = null;
}
return isServerRunning;
}

方法二:

/**
* 命令行模式启动appium, 发现又是每次需要安装appium键盘,unlock 等等
* 暂时没有很好的办法解决这个问题
*
*/
public void startServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4723 --session-override -dc \"{\"\"noReset\"\": \"\"true\"\"}\"\"");
Thread.sleep(Constant.LONGTIME_IN_MS);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
} public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
runtime.exec("taskkill /F /IM cmd.exe");
} catch (IOException e) {
e.printStackTrace();
}
}

方法三:(注意别有中午路径,可以自行转换下)

public void startServer() {
CommandLine cmd = new CommandLine("C:\\Program Files (x86)\\Appium\\node.exe");
cmd.addArgument("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\Appium.js");
cmd.addArgument("--address");
cmd.addArgument("127.0.0.1");
cmd.addArgument("--port");
cmd.addArgument("4723"); DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(cmd, handler);
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
} public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
上一篇:mysql更新(五) 完整性约束 外键的变种 三种关系 数据的增删改


下一篇:启动多个appium服务(同时运行多台设备)