Search code examples
javaandroidseleniumappium

Java Selenium and Appium doesn't work Could not start REST http interface listener


I using Java, selenium and appium for automation testing web application on android

I use this code:

        service = AppiumDriverLocalService.buildDefaultService();
        service.start();
        if (service == null || !service.isRunning())
            throw new AppiumServerHasNotBeenStartedLocallyException("An appium server node is not started!");
        DesiredCapabilities capabilitiesAndroid = new DesiredCapabilities();
        capabilitiesAndroid.setCapability("deviceName", "Android Emulator");
        capabilitiesAndroid.setCapability("deviceOrientation", "portrait");
        capabilitiesAndroid.setCapability("platformVersion", "8.0.0");
        capabilitiesAndroid.setCapability("platformName", "Android");
        capabilitiesAndroid.setCapability("browserName", "Chrome");
        driver = new AndroidDriver<WebElement>(capabilitiesAndroid);

With the same parameter I can open new session in appium. In Java I got 2 times this error:

[Appium] Welcome to Appium v1.6.5
[HTTP] Could not start REST http interface listener. The requested port may already be in use. Please make sure there is no other instance of this server running already.

Error: listen EADDRINUSE 0.0.0.0:4723
    at Object._errnoException (util.js:1041:11)
    at _exceptionWithHostPort (util.js:1064:20)
    at Server.setupListenHandle [as _listen2] (net.js:1322:14)
    at listenInCluster (net.js:1370:12)
    at doListen (net.js:1492:7)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

I haven't started process node.exe. Anyone know why it doesn't work?


Solution

  • You can not use the same port to initialize one more instance. You can initialize with different port. Your code tries to initialize the instance with same port number every time. You can use the below code to solve the issue.

     service = AppiumDriverLocalService.buildService( new
          AppiumServiceBuilder().usingDriverExecutable(new File(
          "C:\\Program Files\\nodejs\\node.exe")) .withAppiumJS(new File(
          "C:\Users\YourUserName\AppData\Roaming\npm\node_modules\appium\build\lib\main.js"
          )) .withIPAddress("127.0.0.1").usingAnyFreePort());
    
         service.start();