Appium does not initialize driver when launched pr

2019-09-12 08:42发布

I am initializing Appium through command line using Java and Selenium for running test on Android chrome browser. However, the process runs for infinite time and the code from "DesiredCapabilities" line doesn't get executed.. Code :

Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");

System.out.println("Android Chrome driver would be used");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);

Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");

I don't get any output in console.

Nothing happens. The process doesn't go on next line (i.e. setting DesiredCapabilities). The chrome doesn't get launched on the device.

Note : When i execute the command from command line, and then start test from DesiredCapabilities line, the test runs fine and chrome is initialized successfully.

What is wrong with the code?

2条回答
Ridiculous、
2楼-- · 2019-09-12 09:02

I think the problem is that you are in loop over the proc output, and you will never get out of there, see you don't even get "Here is the standard error of the command (if any):\n"

i'd put the output logging on another thread so that your program can go on, the following code can be improved but should get the job done:

Process proc;
String path_to_appium = System.getenv("APPIUM_HOME") + File.separator + "node_modules" + File.separator + "appium" + File.separator + "bin" + File.separator + "appium.js";
String path_to_node = System.getenv("APPIUM_HOME") + File.separator + "node.exe";
proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");

BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

new Thread()
{
    public void run() {
        // read thae output from the command
        System.out.println("Here is the standard output of the command:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    }
}.start();

System.out.println("Android Chrome driver would be used");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName", "HTC One X");
capabilities.setCapability("platformVersion", "4.2.2");
capabilities.setCapability("device", "android");
capabilities.setCapability("browserName", MobileBrowserType.CHROME);

Thread.sleep(2000);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
查看更多
神经病院院长
3楼-- · 2019-09-12 09:09

The problem was there in the latest appium version i.e. 1_4_16_1.

When the appium server was launched programatically, it was creating a deadlock because of which the driver was not being initialized.

The issue got solved after using ServerArguments of Appium and replacing the line

proc = Runtime.getRuntime().exec("\"" + path_to_node + "\"" + " " + "\"" + path_to_appium + "\"" + " " + "--address 127.0.0.1 --browser-name Chrome --platform-name Android --platform-version 17 --automation-name Appium --chromedriver-port 9516 --bootstrap-port 4724 --no-reset --local-timezone --log appium_log.log");

with below code :

            ServerArguments serverArguments = new ServerArguments();
            serverArguments.setArgument("--address","127.0.0.1");
            serverArguments.setArgument("--chromedriver-port", 9516);
            serverArguments.setArgument("--bootstrap-port", 4724);
            serverArguments.setArgument("--browser-name", "Chrome");
            serverArguments.setArgument("--no-reset", true);
            serverArguments.setArgument("--local-timezone", true);
            AppiumServer appiumServer = new AppiumServer(appium_folder, serverArguments);
            appiumServer.startServer();
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("test.com");
查看更多
登录 后发表回答