一.概述
selenium在編寫頁面中函數(shù)的時候,需要調(diào)用的某些方法是很多PageObject都需要調(diào)用到的,而且這些原始的方法需要進(jìn)行封裝
以便使腳本更好的實現(xiàn)功能,那么有哪些方法是需要經(jīng)常用到的呢?
二.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 * 判斷對象是否沒出現(xiàn)
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 }