Appium 能够查看和查找屏幕上未显示的元素
我正在尝试构建一个测试自动化项目,我希望我的驱动程序向下滚动 然后执行一些操作。但由于某种原因,appium 即使不向下滚动也能够找到元素。我不确定 appium 如何识别不在屏幕上且只有向下滚动时肉眼可见的元素。有类似问题的人找到了解决方法吗?
我使用 ExpectedCondition.visibilityOF(element) 来确定元素是否在屏幕上可见
public boolean verifyCoverage(String coverage, String value, String type) throws IOException, InterruptedException {
int counter = 0;
for (int i = 0; i < 15; i++) {
AndroidElement element = (AndroidElement) driver.findElementByAndroidUIAutomator("UiSelector().textContains(\"" + coverage + "\")");
//WebElement coverageOption= driver.findElementByXPath("//android.widget.Button[contains(text(),'"+coverage+"')]");
if (AndroidUtilities.waitForVisibility(driver, element)) {
return true;
}
else {
System.out.println ("Cannot see");
return false;
}
}
public static boolean waitForVisibility(AndroidDriver<WebElement> driver, AndroidElement AndroidElement){
try{
// driver.findElementByAndroidUIAutomator("UiSelector().resourceId(\""+targetResourceId+"\")");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(AndroidElement));
boolean isElementPresent = AndroidElement.isDisplayed();
return isElementPresent;
}catch(Exception e){
boolean isElementPresent = false;
System.out.println(e.getMessage());
return isElementPresent;
}
}
最佳答案
作为答案,我建议您使用 visibilityOfElementLocated
而不是 visibilityOf
。
另外,如果您想检查某个元素是否存在而不出现异常,请尝试采用该方法:
if (!((AndroidDriver)driver).findElementsByAndroidUIAutomator("UiSelector().textContains(\"" + coverage + "\")").isEmpty()) {
//some logic when element is located
} else {
//scroll to the particular element
}
关于java - Appium 能够看到屏幕上显示之外的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242679/