以编程方式启动时,Appium 不会初始化驱动程序
Appium does not initialize driver when launched programatically
我正在通过命令行使用 Java 和 Selenium 初始化 Appium,以便在 Android chrome 浏览器上进行 运行 测试。
但是,该过程会无限期运行,并且“DesiredCapabilities
”行中的代码不会执行。
代码:
Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
System.out.println("Android Chrome driver would be used");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);
Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
我在控制台中没有得到任何输出。
没有任何反应。该过程不会在下一行继续(即设置 DesiredCapabilities
)。 chrome 未在设备上启动。
注意 :当我从命令行执行命令,然后从 DesiredCapabilities
行开始测试时,测试运行正常并且 chrome 被初始化成功。
代码有什么问题?
我认为问题是你在 proc 输出上循环,你永远不会离开那里,看你甚至没有 "Here is the standard error of the command (if any):\n"
我会将输出记录放在另一个线程上,以便您的程序可以继续,以下代码可以改进但应该完成工作:
Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
new Thread()
{
public void run() {
// read thae output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
}.start();
System.out.println("Android Chrome driver would be used");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);
Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
问题出现在最新的 appium 版本中,即 1_4_16_1。
当 appium 服务器以编程方式启动时,它正在创建一个死锁,因为驱动程序没有被初始化。
使用 Appium 的 ServerArguments
并替换行
后问题得到解决
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
使用以下代码:
ServerArguments serverArguments = new ServerArguments();
serverArguments.setArgument("--address","127.0.0.1");
serverArguments.setArgument("--chromedriver-port", 9516);
serverArguments.setArgument("--bootstrap-port", 4724);
serverArguments.setArgument("--browser-name", "Chrome");
serverArguments.setArgument("--no-reset", true);
serverArguments.setArgument("--local-timezone", true);
AppiumServer appiumServer = new AppiumServer(appium_folder, serverArguments);
appiumServer.startServer();
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
我正在通过命令行使用 Java 和 Selenium 初始化 Appium,以便在 Android chrome 浏览器上进行 运行 测试。
但是,该过程会无限期运行,并且“DesiredCapabilities
”行中的代码不会执行。
代码:
Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
System.out.println("Android Chrome driver would be used");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);
Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
我在控制台中没有得到任何输出。
没有任何反应。该过程不会在下一行继续(即设置 DesiredCapabilities
)。 chrome 未在设备上启动。
注意 :当我从命令行执行命令,然后从 DesiredCapabilities
行开始测试时,测试运行正常并且 chrome 被初始化成功。
代码有什么问题?
我认为问题是你在 proc 输出上循环,你永远不会离开那里,看你甚至没有 "Here is the standard error of the command (if any):\n"
我会将输出记录放在另一个线程上,以便您的程序可以继续,以下代码可以改进但应该完成工作:
Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
new Thread()
{
public void run() {
// read thae output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
}.start();
System.out.println("Android Chrome driver would be used");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);
Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
问题出现在最新的 appium 版本中,即 1_4_16_1。
当 appium 服务器以编程方式启动时,它正在创建一个死锁,因为驱动程序没有被初始化。
使用 Appium 的 ServerArguments
并替换行
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");
使用以下代码:
ServerArguments serverArguments = new ServerArguments();
serverArguments.setArgument("--address","127.0.0.1");
serverArguments.setArgument("--chromedriver-port", 9516);
serverArguments.setArgument("--bootstrap-port", 4724);
serverArguments.setArgument("--browser-name", "Chrome");
serverArguments.setArgument("--no-reset", true);
serverArguments.setArgument("--local-timezone", true);
AppiumServer appiumServer = new AppiumServer(appium_folder, serverArguments);
appiumServer.startServer();
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");