我是使用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");
}
}
}
这只是一个示例测试代码,我正试图找出为什么我找不到元素。预先感谢
答案 0 :(得分:0)
您需要以所需的功能提供应用程序包和应用程序活动。
示例:
cap.setCapability(“ appPackage”,“ com.android.app1”);
cap.setCapability(“ appActivity”,“ com.android.app1.MainActivity”);