【发布时间】:2019-10-05 20:42:44
【问题描述】:
首先,我需要检查元素 com.simplemobiletools.gallery:id/text_view 是否在 2-3 秒的时间范围内显示。其次,如果显示IS,点击元素android:id/button2,如果显示ISNT,则继续运行代码。
我用什么命令来做到这一点?提前谢谢你。
【问题讨论】:
标签: java android automation appium
首先,我需要检查元素 com.simplemobiletools.gallery:id/text_view 是否在 2-3 秒的时间范围内显示。其次,如果显示IS,点击元素android:id/button2,如果显示ISNT,则继续运行代码。
我用什么命令来做到这一点?提前谢谢你。
【问题讨论】:
标签: java android automation appium
创建如下方法:
public boolean isElementDisplayed(MobileElement el){
try{
return el.isDisplayed();
}catch(Exception e){
return false;
}
}
然后你可以通过调用上面的方法检查元素是否显示:
MobileElement element = driver.findElementById('com.simplemobiletools.gallery:id/text_view');
boolean isElementVisible = isElementDisplayed(element);
if(isElementVisible){
//click element
driver.findElementById("android:id/button2").click();
}else{
//element is not visible... continue Test
}
如果不使用try catch,则在找不到元素时会抛出异常。
【讨论】:
一个。等待元素 3 秒,根据需要轮询 1 秒或更短时间
b.如果发生异常意味着元素在 3 秒内不显示
c。检查 webelement 是否不为空,然后单击它
try {
WebElement textView =
driver.findElement(By.id("com.simplemobiletools.gallery:id/text_view"));
new FluentWait<WebElement>(textView)
.withTimeout(3, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.until(new Function<WebElement, Boolean>() {
@Override
public Boolean apply(WebElement element) {
return element.isDisplayed();
}
});
}
catch(TimeoutException e){
System.out.println("TimeoutException exception occured after 3 sec);
}
if (textView !=null){
// we do not bother about time here,
// just in case findElement returned something we can click here
driver.findElement(By.id("android:id/button2")).click();
}
【讨论】: