我正在尝试编写我的第一个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设备。
答案 0 :(得分:2)
以下是最佳解决方案,您无需每次都手动启动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。
e.g。 APPIUM_BINARY_PATH = C:\用户\ YourUserName \应用程序数据\漫游\ NPM \ node_modules \ appium \建立\ lib中\ main.js
有关如何设置APPIUM_BINARY_PATH的更多详细信息,请参阅以下屏幕截图: