gpt4 book ai didi

java - Appium:连接到 127.0.0.1:4723 [/127.0.0.1] 失败:连接被拒绝:连接

转载 作者:行者123 更新时间:2023-12-02 03:07:35 30 4
gpt4 key购买 nike

我正在尝试编写我的第一个 Java Appium 测试。
每次运行代码时都会出现以下错误:

org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect  
....
at MyFirstTest.setupTest(MyFirstTest.java:52)
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)

这是我的代码:

public class MyFirstTest  
{
public AppiumDriver<MobileElement> driver;
public WebDriverWait wait;
String usersEmail = "xxxx@gmail.com";
String userPassword = "aaaaaa";

public void login() throws InterruptedException
{
WebElement email = driver.findElement(By.xpath("//android.widget.EditText[contains(@text,'Enter Email')]"));
email.sendKeys(usersEmail);
Sleeper.SYSTEM_SLEEPER.sleep(new Duration(1000, TimeUnit.MILLISECONDS));
driver.findElement(By.xpath("//android.widget.RelativeLayout[contains(@resource-id,'edtPassword')]//android.widget.EditText[contains(@resource-id,'edtText')]")).sendKeys(userPassword);
driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'btnLogin')]")).click();
}
@Before
public void setupTest() throws Exception
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platormName", "Android");
capabilities.setCapability("deviceName", "Redmi");
capabilities.setCapability("appPackage","com.ls.stockpair");
capabilities.setCapability("appActivity","com.ls.stockpair.activity.SplashActivity");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(8000);
}
@Test
public void go() throws InterruptedException
{

WebElement email = driver.findElement(By.xpath("//android.widget.EditText[contains(@text,'Enter Email')]"));
email.sendKeys(usersEmail);
Sleeper.SYSTEM_SLEEPER.sleep(new Duration(1000, TimeUnit.MILLISECONDS));
driver.findElement(By.xpath("//android.widget.RelativeLayout[contains(@resource-id,'edtPassword')]//android.widget.EditText[contains(@resource-id,'edtText')]")).sendKeys(userPassword);
driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'btnLogin')]")).click();

Sleeper.SYSTEM_SLEEPER.sleep(new Duration(2000, TimeUnit.MILLISECONDS));
}
}

目前,我在运行代码之前手动启动 Appium。还尝试从代码启动 Appium - 这没有帮助。
这段代码在我开始编写这段代码的第一天就起作用了,但是不知何故(我真的不知道为什么)它停止了工作。
我在网上看到了很多关于这个问题的不同答案的问题,但到目前为止没有人帮助我。
我正在使用通过 USB 连接到我的 Windows 计算机的真实 Android 设备。

最佳答案

下面是最好的解决方案,你不需要每次都手动启动appium。

public static AndroidDriver<MobileElement> driver=null;
public static AppiumDriverLocalService service=null;

service = AppiumDriverLocalService.buildDefaultService();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platormName", "Android");
capabilities.setCapability("deviceName", "Redmi");
capabilities.setCapability("appPackage","com.ls.stockpair");
capabilities.setCapability("appActivity","com.ls.stockpair.activity.SplashActivity");

service.start();
driver = new AndroidDriver<MobileElement>(service, capabilities);

注意:如果您使用的是appium版本1.5.3+,那么您需要在环境变量中设置APPIUM_BINARY_PATH。

例如APPIUM_BINARY_PATH=C:\Users\YourUserName\AppData\Roaming\npm\node_modules\appium\build\lib\main.js

有关如何设置 APPIUM_BINARY_PATH 的更多详细信息,请参阅下面的屏幕截图:

APPIUM_BINRAYPATH

关于java - Appium:连接到 127.0.0.1:4723 [/127.0.0.1] 失败:连接被拒绝:连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41548086/

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