[英]Run the appium server programmatically
我正在尝试以编程方式运行Appium服务器。 我尝试了Appium提供的所有可能选项,但没有得到任何结果
从命令行,如果我尝试使用C:\\Users\\User>appium
它将启动服务器。 如何使用Java代码执行相同操作?
注意: Appium的版本是1.6.5
AppiumDriverLocalService service=AppiumDriverLocalService.buildDefaultService();
service.start();
这是我用来以编程方式运行appium服务器的代码。
我得到的错误是
java.lang.NoClassDefFoundError:org / apache / commons / validator / routines / InetAddressValidator
你尝试过这个吗
import java.io.*;
public class CmdTest {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("appium");
}
}
这是使用Java代码以编程方式启动和停止appium服务器的方法
import java.nio.file.Paths;
public class AppiumSetupAndTearDown {
public static void startAppiumServer() throws IOException, InterruptedException {
String logDir = <the path where you want to create the appium output file>;
File appiumOutput = new File(logDir);
if (!appiumOutput.exists()) {
boolean status = appiumOutput.createNewFile();
if (!status) {
throw new IOException("Failed to create Appium output file!");
}
}
String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/startAppium.sh"};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectError(new File(logDir));
pb.redirectInput(new File(logDir));
pb.redirectOutput(new File(logDir));
pb.start();
System.out.println("Appium server started!");
}
public static void stopAppiumServer() throws IOException, InterruptedException {
String cmd[] = {"/bin/bash", Paths.get(System.getProperty("user.dir")).getParent().getParent().toString() + "/Modules/Shared/stopAppium.sh"};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.start();
System.out.println("Appium server stopped!");
}
}
您还可以在启动和停止appium服务器之后添加Thread.sleep(5000),以防遇到计时问题。
我知道您要使用Java,但是我使用C#以编程方式进行此操作。 因此,我粘贴了我的解决方案以防万一它有助于获得一个想法:
C#代码:
public void startAppiumServerLocally()
{
try
{
string nodejs = "\"" + Environment.GetEnvironmentVariable("nodejs") + "\""; // Environment path for node.exe
string appium = "\"" + Environment.GetEnvironmentVariable("appium") + "\""; // Environment path for main.js or appium.js
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(nodejs);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError = true;
myProcessStartInfo.CreateNoWindow = true;
// getOverride() method returns '--session-override'
// getDesiredCapabilitiesJson() returns a JSON with some capabilities '--default-capabilities {"udid":identifier..., deviceName: "Samsung S7"....}'
// getDriverPort() returns --port X just in case I need to start in a different and unused port.
string args = appium + getOverride() + getDesiredCapabilitiesJson() + getDriverPort();
myProcessStartInfo.Arguments = args;
nodeProcess = new Process();
nodeProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
nodeProcess.StartInfo = myProcessStartInfo;
nodeProcess.Start();
nodeProcess.BeginErrorReadLine();
StreamReader myStreamReader = nodeProcess.StandardOutput;
while (_bAppiumForceEnd == false)
{
if (_bAppiumInit == false)
{
string line = myStreamReader.ReadLine();
if (line.Contains("listener started"))
{
// _bAppiumInit is a flag to start my tests once the server started
_bAppiumInit = true;
}
}
else
{
myStreamReader.ReadLine();
}
}
myStreamReader.Close();
}
catch (Exception e)
{
//Log(e.Message);
}
}
我知道创建一个while循环不是最好的解决方案,但是有时我的nodeProcess并没有停止...所以我将其更改为这种方式,并且效果很好。
确保您已全局安装Appium ...
npm install -g appium
尝试将Commons Validator lib添加到您的classpath / pom.xml(commons-validator-1.4.0.jar)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.