Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start new session when using appium
Getting error always when running code for Appium server that is running on my machine. Can anyone help me with this. I followed the threads but none of them were able to solve my problem.
If I manually run "appium.exe" and running the code works fine, but when I run appium from the code and the interoperability causes a problem.
Configurations: Windows 7 64 bit, Appium 1.2.4.1, Selenium 2.45, Appium Java client - 2.2, java 1.6
Below is the code for starting the Appium software server
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();
}
}
}
My script for mobile is
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();
Getting the following Exceptions,
Exception on thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Failed to start a new session. Possible reasons are the wrong address of the remote server or the unsuccessful launch of the browser. Build info: version: '2.45.0', version: '5017cb8', time: '2015-02-26 23:59:50' System info: host: 'Saravanan-PC', ip: '192.168.101.22' , os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_45' Driver info: driver.version: AndroidDriver at org.openqa.selenium .remote.RemoteWebDriver.execute (RemoteWebDriver.java:593) at io.appium.java_client.AppiumDriver.execute (AppiumDriver.java:180) at org.openqa.selenium.remote.RemoteWebDriver.startSession.RemoteWebDriver :startSession.240) at org.openqa.selenium.remote.RemoteWebDriver. (RemoteWebDriver.java:126) at org.openqa.selenium.remote.RemoteWebDriver. (RemoteWebDriver.java:153) in io.appium.java_client.AppiumDriver. (AppiumDriver.java:109) in io.appium.java_client.android.AndroidDriver. (AndroidDriver.java:40) at com.helloappium.HelloAppium.main (HelloAppium.java:48) Reason: org.apache.http.conn.HttpHostConnectException: Connection to 127.0.0.1:4723 [/127.0.0.1] failed: connection rejected: connect at org.apache.http.impl.conn.HttpClientConnectionOperator.connect (HttpClientConnectionOperator.java:142) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect3) org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect3 .http.impl.execchain.MainClientExec.establishRoute (MainClientExec.java:363) at org.apache.http.impl.execchain.MainClientExec.execute (MainClientExec.java:219) at org.apache.http.impl.execchain.ProtocolExec.execute (ProtocolExec.java:195) at org.apache.http .impl.execchain.RetryExec.execute (RetryExec.java:86) at org.apache.http.impl.execchain.RedirectExec.execute (RedirectExec.java:108) at org.apache.http.impl.client.InternalHttpClient.doExecute (InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:72) at org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttp:57Client) org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute (ApacheHttpClient.java:126) at org.openqa.selenium.remote.internal.ApacheHttpClient.execute (ApacheHttpClient.java.openqa.selenium.remote.HttpCommandExecutor.execute (HttpCommandExecutor.java:133) at org.openqa.selenium.remote.RemoteWebDriver.execute (RemoteWebDriver.java:572) ... 7 more Reason: java.net.ConnectException : connect to java.net.PlainSocketImpl.socketConnect (native method) to java.net.PlainSocketImpl.doConnect (PlainSocketImpl.java:351) to java.net.PlainSocketImpl.connectToAddress (PlainSocketImpl.java.net:213) to java.net.Plainpl .connect (PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:366) at java.net.Socket.connect (Socket.java:529) at org.apache.http.conn.socket. PlainConnectionSocketFactory.connectSocket (PlainConnectionSocketFactory.java:72) at org.apache.http.impl.conn.HttpClientConnectionOperator.connect (HttpClientConnectionOperator.java:125) ... 20 more [36minfo [39m: Welcome to Appium v1.2.4 (REV 71584425ecf777349e3215f0f18e71e7782acfb6) [36minfo [39m: http listener: Appium REST.1 http4723 [127.0.0 39m: [debug] Server default arguments: {"address": "127.0.0.1", "noReset": true, "log": "F: \ Software \ Selenium \ Appium \ appiumServerLogs.txt", "localTimezone": true, "selendroidPort": 8082} [36minfo [39m: Console LogLevel: debug [36minfo [39m: LogLevel file: debug4723 [36minfo [39m: [debug] Server default arguments: {"address": "127.0.0.1", "noReset": true, "log": "F: \ Software \ Selenium \ Appium \ appiumServerLogs.txt", "localTimezone": true, "selendroidPort": 8082} [36minfo [39m: Console LogLevel: debug [36minfo [39m: LogLevel file: debug4723 [36minfo [39m: [debug] Server default arguments: {"address": "127.0.0.1", "noReset": true, "log": "F: \ Software \ Selenium \ Appium \ appiumServerLogs.txt", "localTimezone": true, "selendroidPort": 8082} [36minfo [39m: Console LogLevel: debug [36minfo [39m: LogLevel file: debug
source to share
I was able to reproduce the problem. The problem arises because it takes a relatively long time for appium. You can see this by copying the command line you created in the cmd command line and running it. Appium takes about 30 seconds. So the dirty solution is just to add Thread.sleep(30000);
after aServer.startServer();
to the mobile script. The best solution is to wait until appium is ready. For this, I think the best approach is to read the output stream and make sure it contains the expected response.
This simple code shows this approach (without checking the answer)
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();
}
}
source to share