【发布时间】:2018-08-21 23:59:30
【问题描述】:
我是使用 Appium 自动化的初学者,我遇到了一个问题,我无法在下面的测试代码中找到一个元素,请帮助我了解我缺少什么。
Java
public class Main {
AndroidDriver driver = null;
@Before
public void main(){
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "00757fccdb6fabc5");
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.APP, "C:\\Users\\kswamina\\Desktop\\zApp1\\zApp1\\zApp1\\build\\outputs\\apk\\debug\\zApp1-debug.apk");
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testClick(){
if(driver != null){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebDriverWait wait0 = new WebDriverWait(driver, 10);
wait0.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.id("com.example.app1:id/tvKey"))).sendKeys("MMS");
driver.findElement(By.id("com.example.app1:id/etName")).sendKeys("VPN");
driver.findElement(By.id("com.example.app1:id/btnStore")).click();
WebDriverWait wait = new WebDriverWait(driver, 2);
WebElement result = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.example.app1:id/tvResult")));
Assert.assertEquals(result.getText(), "Set data successfull");
}
}
}
我收到错误消息“预期条件失败:等待元素可见性”
作为一种解决方法,在应用程序启动后,如果我按下主页按钮->导航到应用程序列表->启动应用程序->我能够找到该元素并对其进行操作,如下所示。
解决代码
public class Main {
AndroidDriver driver = null;
@Before
public void main(){
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "00757fccdb6fabc5");
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.APP, "C:\\Users\\kswamina\\Desktop\\zApp1\\zApp1\\zApp1\\build\\outputs\\apk\\debug\\zApp1-debug.apk");
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testClick(){
if(driver != null){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.pressKey(new KeyEvent(AndroidKey.HOME));
driver.findElementByAccessibilityId("Apps").click();
driver.findElementByAccessibilityId("App1").click();
WebDriverWait wait0 = new WebDriverWait(driver, 10);
wait0.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.id("com.example.app1:id/tvKey"))).sendKeys("MMS");
driver.findElement(By.id("com.example.app1:id/etName")).sendKeys("VPN");
driver.findElement(By.id("com.example.app1:id/btnStore")).click();
WebDriverWait wait = new WebDriverWait(driver, 2);
WebElement result = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.example.app1:id/tvResult")));
Assert.assertEquals(result.getText(), "Set data successfull");
}
}
}
这只是一个示例测试代码,我只是想找出为什么我无法找到一个元素。提前致谢
【问题讨论】:
-
你能在不按主页按钮的情况下共享服务器日志吗
标签: appium appium-android