简体   繁体   中英

Starting Appium server programmatically

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();
        }
   }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM