在运行在我的计算机上运行的Appium服务器的代码时始终出错。任何人都可以帮助我解决这个问题。我曾经遵循过一些线索,但没有一个线程无法解决我的问题。
如果我手动运行" appium.exe" &安培;运行代码工作正常,但当我开始从代码&运行appium时互动是一个问题。
配置:Windows 7 64位,Appium 1.2.4.1,Selenium 2.45,Appium Java客户端 - 2.2,java 1.6
public class AppiumServerUtils {
public void startServer(){
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("F:\\Softwares\\Selenium\\Appium\\AppiumForWindows-1.2.4.1\\Appium\\node.exe");
command.addArgument("F:\\Softwares\\Selenium\\Appium\\AppiumForWindows-1.2.4.1\\Appium\\node_modules\\appium\\bin\\appium.js");
command.addArgument("--address");
command.addArgument("127.0.0.1",false);
command.addArgument("--port",false);
command.addArgument("4723",false);
command.addArgument("--bootstrap-port",false);
command.addArgument("4724",false);
command.addArgument("--selendroid-port",false);
command.addArgument("8082",false);
command.addArgument("--no-reset",false);
command.addArgument("--local-timezone");
command.addArgument("--log");
command.addArgument("F:\\Softwares\\Selenium\\Appium\\appiumServerLogs.txt");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopServer(){
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("taskkill");
command.addArgument("/F");
command.addArgument("/IM");
command.addArgument("node.exe");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
}
static AndroidDriver driver;
AppiumServerUtils aServer = new AppiumServerUtils();
aServer.startServer();
DesiredCapabilities mDesiredCapabilities = new DesiredCapabilities();
mDesiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
mDesiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus S");
mDesiredCapabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.vod");
mDesiredCapabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.vod.launcher.Main");
mDesiredCapabilities.setCapability(MobileCapabilityType.APP_WAIT_ACTIVITY, "com.android.vod.tools.remotecontrol.dialogs.Authentication");
mDesiredCapabilities.setCapability(MobileCapabilityType.VERSION, "4.3");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), mDesiredCapabilities);
androidDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
androidDriver.hideKeyboard();
driver.close();
线程中的异常" main" org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器的无效地址或浏览器启动失败。 构建信息:版本:' 2.45.0',修订版:' 5017cb8',时间:' 2015-02-26 23:59:50' 系统信息:主持人:' Saravanan-PC',ip:' 192.168.101.22',os.name:' Windows 7',os.arch:&# 39; x86',os.version:' 6.1',java.version:' 1.6.0_45' 驱动程序信息:driver.version:AndroidDriver 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593) at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180) 在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) 在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:126) 在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:153) 在io.appium.java_client.AppiumDriver。(AppiumDriver.java:109) 在io.appium.java_client.android.AndroidDriver。(AndroidDriver.java:40) 在com.helloappium.HelloAppium.main(HelloAppium.java:48) 引起:org.apache.http.conn.HttpHostConnectException:连接到127.0.0.1:4723 [/127.0.0.1]失败:连接被拒绝:连接 在org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:142) 在org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) 在org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) 在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) 在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) 在org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126) 在org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72) 在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572) ......还有7个 引起:java.net.ConnectException:连接被拒绝:连接 at java.net.PlainSocketImpl.socketConnect(Native Method) 在java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) 在java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 在java.net.Socket.connect(Socket.java:529) at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72) 在org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125) ......还有20个 [36minfo [39m:欢迎来到Appium v1.2.4(REV 71584425ecf777349e3215f0f18e71e7782acfb6) [36minfo [39m:Appium REST http接口监听器在127.0.0.1:4723上启动 [36minfo [39m:[debug]非默认服务器args:{"地址":" 127.0.0.1"," noReset":true,&#34 ;日志":" F:\软件\硒\ Appium \ appiumServerLogs.txt"" localTimezone":真," selendroidPort":8082} [36minfo [39m:Console LogLevel:debug [36minfo [39m:File LogLevel:debug
答案 0 :(得分:0)
我设法重现了这个问题。出现问题是因为appium需要相对较长的时间才能启动。如果将您构建的命令行复制到cmd提示符并启动它,则可以看到它。 appium需要大约30秒才能准备好。因此,脏解决方案只是在移动脚本中Thread.sleep(30000);
之后添加aServer.startServer();
。更好的解决方案是等到appium准备就绪。为此,我认为最好的方法是 - 读取输出流并验证它是否包含预期的响应。
这个简单的代码显示了这种方法(没有响应验证)
InputStream is = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
executor.getStreamHandler().setProcessOutputStream(is);
try {
executor.execute(command, resultHandler);
for (int i=1; i<10; i++) {
int nRead = is.read();
if(nRead!=0)
break;
Thread.sleep(5000);
}
}catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
}