1
votes

So i am using below code as a try to start the Appium server automatically but getting the connection refused:connect error. I am using Maven with Testng

Logs:

FAILED CONFIGURATION: @BeforeTest capabilities org.openqa.selenium.WebDriverException: Connection refused: connect Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:03:16.397Z' System info: host: 'MEL01-ULPT027', ip: '172.24.80.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_152' Driver info: driver.version: AndroidDriver at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:141) at java.util.Optional.orElseGet(Unknown Source) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:140) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42) 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:219) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:142) at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:38)

Code:

public class Startup {


 RemoteWebDriver driver = null;

    public void startAppiumServer() throws IOException, InterruptedException {   

        CommandLine command = new CommandLine("cmd");
        command.addArgument("/c");

        command.addArgument("C:\\Program Files (x86)\\Appium\\node.exe");  
        command.addArgument("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\lib\\appium.js");  
        command.addArgument("--address", false);  
        command.addArgument("127.0.0.1");  
        command.addArgument("--port", false);  
        command.addArgument("4723");  
        command.addArgument("--full-reset", false);
        command.addArgument("--bootstrap-port",false);
        command.addArgument("4724",false);
        command.addArgument("--selendroid-port",false);
        command.addArgument("8082",false);

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
        DefaultExecutor executor = new DefaultExecutor();  
        // executor.setExitValue(1);

        InputStream is = new InputStream() {

            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();
        }
   }
1
A few suggestions: Do not use appium.js, use main.js. Why are you using the last four parameters? Also, you might want to use 0.0.0.0 instead of 127.0.0.1Bill Hileman
Thanks ,just changed the way for starting appium. Now i am creating a bat file and using Java Runtime command i am starting appium server. However on other side now i am struggling with the code for shutting down appium server programmatically. Any suggestions?shashank shekhar

1 Answers

0
votes

I suggest to avoid running Appium from command line, your code won't work on Linux or different OS. So that use AppiumDriverLocalService to start the server. You can find here useful ways to start your server.