Selenium之常用的方法的封装

发表于:2016-1-12 11:25

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:li_wu    来源:51Testing软件测试网采编

  一.概述
  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     }
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号