Connection time out while starting appium server via code

2.5k views Asked by At

I am facing below mentioned connection time out issue while starting appium server from my code:- This exception is coming on daily basis for the first time as i tried to run my code and after that i resolved automatically and it's working fine foe the whole day, but on every day in the morning i am getting this error for very time on daily basis as i tried to run mu code:-

*Please suggest what's wrong!!

My code for starting appium server:-

public static String nodePath="D:\\appium_new\\Appium\\node.exe";
    public static String appiumJsPath="D:\\appium_new\\Appium\\node_modules\\appium\\bin\\appium.js";
    public static String cmd=nodePath+" " +appiumJsPath;
    public  void appServer_Start() throws IOException, InterruptedException{
        p=Runtime.getRuntime().exec(cmd);
        Thread.sleep(20000);
        if (p!=null) {
            System.out.println("Server started successfully");
        }

    }


public void appServer_Stop(){
    if (p!=null) {
        p.destroy();
        System.out.println("Server stopped successfully");

    }
}


DesiredCapabilities cap=new DesiredCapabilities();
    cap.setCapability("platformName", "Android");
    cap.setCapability("platformVersion", "5.1");
    cap.setCapability("deviceName", "Emulator");
    cap.setCapability("app", app.getAbsolutePath());
    cap.setCapability("appPackage", "com.shiprocket.shiprocket");
    cap.setCapability("appActivity", "com.shiprocket.shiprocket.activity.CarouselActivity");
    Thread.sleep(10000);
    driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'SureshSharma-PC', ip: '169.254.246.124', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: AndroidDriver
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:84)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
    at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:36)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:114)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:132)
    at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:92)
    at backendServer.Server.BackendServerStart(Server.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:326)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:142)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:88)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:108)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:64)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:69)
    ... 31 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
    ... 46 more
2

There are 2 answers

1
Shubham Jain On

Your appium server is not started

OR

your URL or port number is not correct in below line of code

ad = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);

The full code will appear like below:

    // Capabilities should be same as declared in Appium settings
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "TestSample");
    capabilities.setCapability("platformName", "Android");

    capabilities.setCapability(CapabilityType.VERSION, "6.0");
    capabilities.setCapability(CapabilityType.PLATFORM, "Windows");

    // application package name
    capabilities.setCapability("app-package", "com.demo.pass");

    // Application start Activity
    capabilities.setCapability("app-activity", "com.demo.pass.acti.SplashScreen");

    capabilities.setCapability("unicodeKeyboard", false);

    ad = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
0
Vinod On

There is a better way of starting appium server through code. For that you need follow below steps:

  1. Install latest stable version of NodeJs (This will also install npm).
  2. Install appium using npm from command line using below command :

    npm install -g appium

  3. After successful installation of appium, add new variable in environment path variable with name as "APPIUM_BINARY_PATH" and value as "C:\Users\YourUserName\AppData\Roaming\npm\node_modules\appium\build\lib\appium.js"

  4. Add ANDROID_HOME environment variable if you have not added previously.

Use the below code snippet which uses inbuilt appium library to start appium server and runs very smoothly.

    public static AndroidDriver<MobileElement> driver;
    public static AppiumDriverLocalService service;
    service = AppiumDriverLocalService.buildDefaultService();
    DesiredCapabilities cap=new DesiredCapabilities();
    cap.setCapability("platformName", "Android");
    cap.setCapability("platformVersion", "5.1");
    cap.setCapability("deviceName", "Emulator");
    cap.setCapability("appPackage", "com.your.sample.app");
    cap.setCapability("appActivity", "com.your.sample.app.YourAppActivity");
    service.start();
    driver = new AndroidDriver<MobileElement>(service, capabilities);