Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused (Connection refused)

The problem

I am trying to run appium server through java code. i am using Mac OS. After running the code appium server starts successfully but on capabilities when it tries to connect to localhost IP Address it fails. if i start appium manually through terminal and then run the code it works perfectly. i have looked into many tutorial but coudnt get any success. Tried all the different ports too.

Here is the code:
CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
command.addArgument("–address", false);
command.addArgument(“127.0.0.1”);
command.addArgument("–port", false);
command.addArgument(“4723”);
command.addArgument("–bootstrap-port", false);
command.addArgument(“4724”);

    //command.addArgument("--full-reset", false);
    //command.addArgument("--session-override", true);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);
    try {
        executor.execute(command, resultHandler);
        Thread.sleep(5000);
        System.out.println("Appium server started.");
    } catch (IOException e) {
        e.printStackTrace();

}

For Capabilities:

DesiredCapabilities capabilities = new DesiredCapabilities();

    //common capabilities

    capabilities.setCapability("appium-version", "1.6.4");
    capabilities.setCapability("platformName", "Android");

    //capabilities for Emulator
    capabilities.setCapability("platformVersion", "7.1.1");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus 7");
    capabilities.setCapability("app", "/Users/tvs00023/Desktop/KwizzAdd.apk");

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

The error is :
org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused (Connection refused)
Build info: version: ‘3.3.1’, revision: ‘5234b32’, time: ‘2017-03-10 09:04:52 -0800’

caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused (Connection refused)

Can anybody help me out in this ?

Hi,

Did you solve the problem?

Thanks

Hi @Umme_Ammara and @Nael_Abd_eljawad,

This is due to:
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);

you don’t need to provide the URL while trying to start appium programmatically, just changed the above code as follows:

driver = new AndroidDriver(capabilities);

1 Like

Hi @Shubham_Agarwal,

Do you mean that new AndroidDriver(capabilities); can work as expected ?
We have to start only one Appium server and it will handle all sessions automatically?
Right now I’m running by port and caps, while running only by caps I’m getting Appium server refused connection although the tests running in parallel.
Can’t figure out how does it work by caps only, how does Appium server allocate the ports?

Thanks,
Nael

try to change the code
CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
command.addArgument("–address", false);
command.addArgument(“127.0.0.1”);
command.addArgument("–port", false);
command.addArgument(“4723”);
command.addArgument("–bootstrap-port", false);
command.addArgument(“4724”);
//command.addArgument("–full-reset", false);
//command.addArgument("–session-override", true);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
resultHandler.waitFor(20000);
System.out.println(“Appium server started.”);
} catch (IOException e) {
e.printStackTrace();
}

Thanks :slight_smile:
As you can see in the driver creation @Shubham_Agarwal wrote, that we don’t need to allocate ports for driver creation. It’s better if we allocate dynamic ports than adding as arguments.