【问题标题】:How to invoke the appium server and emulator via Java code in windows machine如何在windows机器中通过Java代码调用appium服务器和模拟器
【发布时间】:2015-08-15 18:38:11
【问题描述】:

我需要使用 Selenium 通过 Java 代码启动 Appium 服务器和模拟器。当我运行 Java 程序时,startappiumserver 方法运行良好。当涉及到 startApplication 方法 (creation of new remoteWebDriver) 我收到如下错误:

线程“主”org.openqa.selenium.remote.UnreachableBrowserException 中的异常:无法启动新会话。 可能的原因是远程服务器地址无效或浏览器启动失败。

代码:

package startup;

import io.appium.java_client.AppiumDriver;

import java.io.IOException;
import java.net.URL;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Startup {

    static RemoteWebDriver driver = null;

    public void startAppiumServer() throws IOException, InterruptedException {   

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

        command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\Appium\\node.exe");  
        command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\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);  

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
        DefaultExecutor executor = new DefaultExecutor();  
        executor.setExitValue(1);
        executor.execute(command, resultHandler);
    }


    public  void stopAppiumServer() throws IOException {  
        CommandLine command = new CommandLine("cmd");  
        command.addArgument("/c");  
        command.addArgument("Taskkill /F /IM node.exe");  

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
        DefaultExecutor executor = new DefaultExecutor();  
        executor.setExitValue(1);  
        executor.execute(command, resultHandler);  

    }

public static  void startApplication() throws Exception{


        System.out.println("application has started...................");

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");            
        capabilities.setCapability("deviceName","emulator-5554");   
        capabilities.setCapability("platformVersion","4.3");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("app","D:/SOFTWARES/Apks/autoInsurancelatest.apk");
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
        System.out.println("Appium SetUp for Android is successful and Appium Driver is launched successfully");
    }

    public static void main(String as[]) throws Exception
    {
        Startup aps = new Startup();

        System.out.println("Starting Server...");

        aps.startAppiumServer();
        System.out.println("Server Started....");
        try{Thread.sleep(5000);}catch(Exception e){}  

        Startup.startApplication();
        try{Thread.sleep(5000);}catch(Exception e){}

        System.out.println("Stopping Server");
        aps.stopAppiumServer();
        System.out.println("Server Stopped");
    }

}

我发现在创建新的remotewebdriver时有一些port错误。我没有找到代码中缺少的内容。

帮我解决这个问题。欢迎提出建议和cmets。

控制台日志:

线程“main”中的异常 org.openqa.selenium.remote.UnreachableBrowserException:不能 开始一个新的会话。可能的原因是远程地址无效 服务器或浏览器启动失败。构建信息:版本:'2.45.0', 修订:'32a636c',时间:'2015-03-05 22:01:35' 系统信息:主机: 'IMPC1388', ip: '172.16.11.2', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51' 驱动信息: driver.version:RemoteWebDriver 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593) 在 org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) 在 org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126) 在 org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153) 在 startup.Startup.startApplication(Startup.java:65) 在 startup.Startup.main(Startup.java:79) 原因: org.apache.http.conn.HttpHostConnectException:连接到 127.0.0.1:4723 [/127.0.0.1] 失败:连接被拒绝:连接 org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:142) 在 org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319) 在 org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) 在 org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) 在 org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) 在 org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) 在 org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) 在 org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) 在 org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72) 在 org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) 在 org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126) 在 org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572) ... 5 更多原因:java.net.ConnectException:连接被拒绝: 在 java.net.DualStackPlainSocketImpl.waitForConnect(Native 方法)在 java.net.DualStackPlainSocketImpl.socketConnect(Unknown 来源)在 java.net.AbstractPlainSocketImpl.doConnect(未知来源) 在 java.net.AbstractPlainSocketImpl.connectToAddress(未知来源) 在 java.net.AbstractPlainSocketImpl.connect(Unknown Source) 在 java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) 在 java.net.Socket.connect(Unknown Source) 在 org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72) 在 org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125) ... 18 更多

【问题讨论】:

  • 你试过 selenium 2.44 jar 版本吗?
  • 我已经尝试了2.44服务器独立jar版本,但仍然存在同样的问题。
  • 那么你应该使用appium驱动而不是远程驱动。您还可以分享您导入的文件列表吗?
  • 感谢您的帮助。我也尝试过使用 Appium 驱动程序。但是最后在Appium服务器启动方法解决了我的问题之后编写了Thread.Sleep()函数。
  • 我明白了..这个问题解决了很好......:)

标签: java selenium appium


【解决方案1】:

您能否对您的代码进行以下更改:

1) 从 bin 目录而不是从 lib 中获取 appium.js

2) 添加以下内容

command.addArgument("--bootstrap-port",false);
command.addArgument("4724",false);
command.addArgument("--selendroid-port",false);
command.addArgument("8082",false);

另外,请确保您在 appium 启动之前有足够的时间。 有关详细信息,请参阅此answer

如果问题仍然存在,请复制代码构建的命令字符串并尝试从 cmd 启动它。

【讨论】:

  • 非常感谢您的回复。我已经在我的代码中完成了上述两个更改。但是,我得到了一个额外的错误,例如 appium.js: error: argument "-p/--port": Expected one argument。空
  • command.addArgument("--address", false); command.addArgument("127.0.0.1"); command.addArgument("--port", false); 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 执行者 = 新的 DefaultExecutor(); executor.setExitValue(1); executor.execute(command, resultHandler);
  • 对不起,我不清楚 - 请提供此代码构建的命令行。您可以在启动它之前打印它
  • CommandLine command = new CommandLine("cmd"); command.addArgument("/c"); command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\Appium\\node.exe"); command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\Appium\\node_modules\\appium\\bin\\appium.js");
  • 如果上面的代码不是你所期望的。请简要说明我需要在这里发布什么样的内容。
猜你喜欢
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
相关资源
最近更新 更多