以下代码为 封装的等待在页面查找元素的方法 ,适用于 webdriver 以及 appium ;在我的项目中目前没有发现 BUG 如有纰漏 请大拿指正,及时修改
/**
*
* @param uiSelector
* @param waitTime
* 秒
* @param bool true 表示必须在当前视图可见(宽值>0) false找到但不一定可见
*/
public WebElement waitThenGetElement(String strSelector, int waitTime, SelectorType type,boolean bool) {
WebElement element = null;
int i = 0;
boolean t1 = true;
while (t1) {
try {
switch (type) {
case UiSelector:
element = ((AndroidDriver) dr).findElementByAndroidUIAutomator(strSelector);
break;
case xPath:
element = dr.findElements(By.xpath(strSelector)).get(0);
break;
case CSS:
element = dr.findElements(By.cssSelector(strSelector)).get(0);
break;
default:
this.pt("waitThenGetElementappear类型错误");
}
TryForWaitReturn ret=tryForWait(element, t1,bool,i,waitTime);;
t1=ret.getT();
element=ret.getEl();
} catch (Throwable e) {
if (catchForWait(i, waitTime,false)) {
break;
}
} finally {
i++;
}
}
// Assert.assertNotNull(element);
return element;
}
/**
* tryForWait 方法返回类型
* @author xian_crazy QQ:330126160
* @version 2014年11月25日 上午10:07:10
* @see
*/
class TryForWaitReturn{
private boolean t;
private WebElement el;
public boolean getT() {
return t;
}
public void setT(boolean t) {
this.t = t;
}
public WebElement getEl() {
return el;
}
public void setEl(WebElement el) {
this.el = el;
}
}
/**
* 判断页面元素是否在当前视图显示
* @see
* @param element
* @param t
* @param bool
* @param i
* @param waitTime
* @return
*/
private TryForWaitReturn tryForWait(WebElement element, boolean t,boolean bool,int i,int waitTime) {
int w = element.getSize().getWidth();
int h=element.getSize().getHeight();
TryForWaitReturn ret=new TryForWaitReturn();
//元素宽度大于零 并且 bool=true 既元素在要在当前视图显示
if (w > 0 && bool) {
t = false;
// this.pt(" get it!");
}
//元素宽度大于或者等于零 并且 bool=false 既元素不一定在当前视图显示
if (w >= 0 && !bool) {
t = false;
// this.pt(" get it!");
if (w == 0) {
this.pt("找到元素但未在当前视图显示");
}
}
//t=false时,说明元素定位到,则打印以下信息
if(!t){
this.pt(" get its H&&W :" +h + "&&" + w);
this.pt(" get its point :" + element.getLocation().getX() + "&&" + element.getLocation().getY());
ret.setT(t);
ret.setEl(element);
return ret;
//t=true时,说明元素找到但w=0,但 bool=true需要等待其在当前视图显示出来,如果超时则认为未找到
}else{
if (catchForWait(i, waitTime,true)) {
t = false;
element=null;
ret.setT(t);
ret.setEl(element);
}
return ret;
}
}
/**
*
* @see
* @param i
* @param waitTime
* @param isfindel true 表示已经找到元素 false 表示没有找到元素
* @return
*/
private boolean catchForWait(int i, int waitTime, boolean isfindel) {
if (i == 0) {
this.pt("等待" + waitTime + "秒……" );
System.out.print(i);
} else if (i == waitTime) {
System.out.print(" " + i);
if(!isfindel){
this.pt(i+"秒后,定位失败");
}else{
this.pt(i+"秒后,已经找到,但其宽度为0");
}
return true;
} else {
System.out.print(" " + i);
}
this.threadSleep(1);
return false;
}
/**
*
* @param uiSelector
* 选择器结果为List
* @param waitTime
* 秒
* @waitElement_S_appear appear说明当前元素在当前页面通过选择器可以找到
* @param bool true 表示元素的宽大于0 false 表示存在元素但是不一定在当前视图
*/
public List<WebElement> waitThenGetElement_S(String strSelector, int waitTime, SelectorType type,boolean bool) {
List<WebElement> list = null;
int i = 0;
boolean t1 = true;
while (t1) {
try {
switch (type) {
case UiSelector:
list = ((AndroidDriver) dr).findElementsByAndroidUIAutomator(strSelector);
break;
case xPath:
list = dr.findElements(By.xpath(strSelector));
break;
default:
this.pt("waitThenElement_S_appear类型错误");
}
WebElement element = list.get(0);
TryForWaitReturn ret=tryForWait(element, t1,bool,i,waitTime);;
t1=ret.getT();
} catch (Throwable e) {
if (catchForWait(i, waitTime,false)) {
break;
}
} finally {
i++;
}
}
return list;
}