一.概述
selenium在编写页面中函数的时候,需要调用的某些方法是很多PageObject都需要调用到的,而且这些原始的方法需要进行封装
以便使脚本更好的实现功能,那么有哪些方法是需要经常用到的呢?
二.selenium封装的常用的方法
1 /**
2 * 判断是否找到对象
3 * @param by
4 * @return
5 */
6 private boolean waitToDisplayed(final By by){
7 boolean waitDisplayed=false;
8 try {
9 waitDisplayed=new WebDriverWait(driver,Config.waitTime).until(new ExpectedCondition<Boolean>(){
10 @Override
11 public Boolean apply(WebDriver d) {
12 // TODO Auto-generated method stub
13 return d.findElement(by).isDisplayed();
14 }
15 });
16
17 } catch (Exception e) {
18 // TODO: handle exception
19 throw new DefinedException(by.toString() + " is not exist until " +Config.waitTime+" sec in file: "+yamlFile);
20 }
21 return waitDisplayed;
22 }
1 /**
2 * 判断对象是否没出现
3 * @param key
4 * @param replace
5 * @return
6 */
7 private boolean waitToNonDisplayed(String key,String[] replace){
8 boolean waitNonDisplayed=false;
9 final By by=this.getBy(key, replace);
10 try {
11 waitNonDisplayed=new WebDriverWait(driver,Config.waitTime).until(new ExpectedCondition<Boolean>(){
12 @Override
13 public Boolean apply(WebDriver d) {
14 // TODO Auto-generated method stub
15 if(d.findElements(by).isEmpty()||!d.findElement(by).isDisplayed()){
16 return true;
17 }else{
18 return false;
19 }
20 }
21 });
22 } catch (Exception e) {
23 // TODO: handle exception
24 throw new DefinedException(by.toString() + " is also exist until " +Config.waitTime+" sec in file: "+yamlFile);
25 }
26 return waitNonDisplayed;
27 }