通过代码启动appium服务器时连接超时

时间:2017-09-08 08:02:15

标签: java selenium-webdriver appium appium-ios appium-android

当我从我的代码启动appium服务器时,我面临下面提到的连接超时问题: - 这个例外是第一次出现,因为我试图运行我的代码,之后我自动解决了它并且它是'我整天都在工作,但是每天早上我都会在每天的时间里遇到这个错误,因为我试图运行mu代码: -

*请说明错误!!

启动appium服务器的代码: -

public static String nodePath="D:\\appium_new\\Appium\\node.exe";
    public static String appiumJsPath="D:\\appium_new\\Appium\\node_modules\\appium\\bin\\appium.js";
    public static String cmd=nodePath+" " +appiumJsPath;
    public  void appServer_Start() throws IOException, InterruptedException{
        p=Runtime.getRuntime().exec(cmd);
        Thread.sleep(20000);
        if (p!=null) {
            System.out.println("Server started successfully");
        }

    }


public void appServer_Stop(){
    if (p!=null) {
        p.destroy();
        System.out.println("Server stopped successfully");

    }
}


DesiredCapabilities cap=new DesiredCapabilities();
    cap.setCapability("platformName", "Android");
    cap.setCapability("platformVersion", "5.1");
    cap.setCapability("deviceName", "Emulator");
    cap.setCapability("app", app.getAbsolutePath());
    cap.setCapability("appPackage", "com.shiprocket.shiprocket");
    cap.setCapability("appActivity", "com.shiprocket.shiprocket.activity.CarouselActivity");
    Thread.sleep(10000);
    driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'SureshSharma-PC', ip: '169.254.246.124', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: AndroidDriver
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:84)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
    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:241)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
    at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:36)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:114)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:132)
    at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:92)
    at backendServer.Server.BackendServerStart(Server.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:326)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:142)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:88)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:108)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:64)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:69)
    ... 31 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
    ... 46 more

2 个答案:

答案 0 :(得分:1)

有一种更好的方法可以通过代码启动appium服务器。为此,您需要按照以下步骤操作:

  1. 安装最新的稳定版NodeJ(这也将安装npm)。
  2. 使用命令行中的npm使用以下命令安装appium:

    npm install -g appium

  3. 成功安装appium后,在环境路径中添加新变量 变量,名称为“APPIUM_BINARY_PATH”,值为“C:\ Users \ YourUserName \ AppData \ Roaming \ npm \ node_modules \ appium \ build \ lib \ appium.js”

  4. 如果您之前未添加,请添加ANDROID_HOME环境变量。

  5. 使用下面的代码片段,它使用内置的appium库来启动appium服务器并且运行非常流畅。

        public static AndroidDriver<MobileElement> driver;
        public static AppiumDriverLocalService service;
        service = AppiumDriverLocalService.buildDefaultService();
        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("platformVersion", "5.1");
        cap.setCapability("deviceName", "Emulator");
        cap.setCapability("appPackage", "com.your.sample.app");
        cap.setCapability("appActivity", "com.your.sample.app.YourAppActivity");
        service.start();
        driver = new AndroidDriver<MobileElement>(service, capabilities);
    

答案 1 :(得分:0)

您的appium服务器未启动。

OR

您的网址或端口号在下面的代码行中不正确

ad = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);

完整代码如下所示:

    // Capabilities should be same as declared in Appium settings
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "TestSample");
    capabilities.setCapability("platformName", "Android");

    capabilities.setCapability(CapabilityType.VERSION, "6.0");
    capabilities.setCapability(CapabilityType.PLATFORM, "Windows");

    // application package name
    capabilities.setCapability("app-package", "com.demo.pass");

    // Application start Activity
    capabilities.setCapability("app-activity", "com.demo.pass.acti.SplashScreen");

    capabilities.setCapability("unicodeKeyboard", false);

    ad = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);