gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:16:46 25 4
gpt4 key购买 nike

在从我的代码启动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

最佳答案

有更好的方法通过代码启动appium服务器。为此,您需要执行以下步骤:

  1. 安装 NodeJs 的最新稳定版本(这还将安装 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 环境变量,请添加它。

使用下面的代码片段,它使用内置的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);

关于java - 通过代码启动appium服务器时连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46111533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com