目的

1. 简化调用

WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:

WebElement element =driver.findElement(By.name("q"));
element.sendKeys("Cheese!");

我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:

protected void sendKeys(By by, String value){
driver.findElement(by).sendKeys(value);
}

那么,在测试用例可以这样调用:

sendKeys(By.name("q"),”Cheese!”);

2. 添加异常捕获或等待

对于一些常用的操作,比如查找元素,我们可以在查找元素前面添加一个显性等待,然后再是查找元素。

protected void findEle(By by){
new WebDriverWait(driver,10).until( ExpectedConditions.presenceOfElementLocated(by));
return driver.findElement(by);
}

样例

封装之后,我们可以把这些封装函数放到公共模块,方便调用。

更普遍的方式是作为BasePage的方法,在POM方式中,其他所有Page都继承自 BasePage,这样所有的Page都可以直接调用这些方法。

比如下面的简单的 BasePage :

import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait; public class BasePage{
protected RemoteWebDriver driver;
protected WebDriverWait driverWait;
private int WAIT_ELEMENT_TO_LOAD=10; protected boolean isWebElementExist(By selector) {
try {
driver.findElement(selector);
return true;
} catch(NoSuchElementException e) {
return false;
}
} protected String getWebText(By by) {
try {
return driver.findElement(by).getText();
} catch (NoSuchElementException e) {
return "Text not existed!";
}
} protected void clickElementContainingText(By by, String text){
List<WebElement>elementList = driver.findElements(by);
for(WebElement e:elementList){
if(e.getText().contains(text)){
e.click();
break;
}
}
} protected String getLinkUrlContainingText(By by, String text){
List<WebElement>subscribeButton = driver.findElements(by);
String url = null;
for(WebElement e:subscribeButton){
if(e.getText().contains(text)){
url =e.getAttribute("href");
break;
}
}
return url;
} protected void click(By by){
driver.findElement(by).click();
driver.manage().timeouts().implicitlyWait(WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
} protected String getLinkUrl(By by){
return driver.findElement(by).getAttribute("href");
} protected void sendKeys(By by, String value){
driver.findElement(by).sendKeys(value);
}
}

Selenium webdriver 封装与重用的更多相关文章

  1. Selenium自动化测试Python四:WebDriver封装

    WebDriver 封装 欢迎阅读WebDriver封装讲义.本篇讲义将会重点介绍Selenium WebDriver API的封装的概念和方法,以及使用封装进行自动化测试的设计. WebDriver ...

  2. Selenium Webdriver——Table类封装

    WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...

  3. 【转载】Selenium WebDriver的简单操作说明

    转载自:http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏 ...

  4. Selenium WebDriver的简单操作说明

    [From] http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才 ...

  5. Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)

            研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...

  6. Selenium WebDriver 之 PageObjects 模式 by Example

    目录 1. 项目配置 2. 一个WebDriver简单例子 3. 使用Page Objects模式 4. 总结 5. Troubleshooting 6. 参考文档 本篇文章通过例子来阐述一下Sele ...

  7. Selenium WebDriver 处理cookie

    在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆. 首先了解一下Java Cookie类的一些方法. 在jsp中处理cookie数据的常用方法: ...

  8. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  9. [selenium webdriver Java]检查元素是否存在

    Selenium WebDriver没有实现Selenium RC的isElementPresent()方法来检查页面上的元素是否存在. 在WebDriver中封装一个类似的方法,如下: public ...

随机推荐

  1. 怎样找Android里自带的数据库?

    首先先让它显示出来. 窗口window下的Show View 下的File Explorer(other里), 这时File Explorer窗口 就会显示出来,下一步我们找到data下的data,然 ...

  2. 排名最重要的三个优化阶段分析 --------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )

    网站排名,不是一两天能够决定的.要想取得好的排名,得分时间分阶段地做排名,网站优化分前期,中期,后期,怎么来区别不同的阶段该用怎样的优化手段.今晚SEO研究中心创始人Moon老师分享:排名最重要的三个 ...

  3. jQuery 表单验证插件——Validation(基础)

    这个插件不错,是用jquery写的.能进行表单验证.我喜欢它的原因是因为 1.他有自带的验证规则 2.你可以自己写验证规则 3.可以通过ajax与后台交互,与后台数据比较.最后返回结果!我在表单中要验 ...

  4. Ci 分页类的所有属性总结

    //#######################自定义分页 $config['uri_segment'] = 3;//分页方法自动测定你 URI 的哪个部分包含页数 $config['num_lin ...

  5. 工时统计的sql练习--包含时间处理

    //按月统计,除去周末的考勤,(工时,请假,缺勤) --建表sql 创建[dbo].[AbsenceHourld]CREATE TABLE [dbo].[AbsenceHourld]( [id] [i ...

  6. (转)ThinkPHP3.0 使用分组后路径访问无效的解决方法!

    注意,清除Runtime,就是清除缓存,很重要,妹的,调试了一下午,总是加上Home目录分组就找不到页面,直接放到action下就行,原来是缓存搞得鬼,另外要在入口文件开启‘APP_DEBUG’ 在T ...

  7. RSA PKCS1padding 填充模式

    在BouncyCastle实现RSA的PKCS1V1.5模式中,如果是公钥加密信息(forEncryption=true),密钥长度为1024位,那么输出的密文块长度为128个字节,输入的明文块长度为 ...

  8. python列表、字典与csv

    在日常数据分析时最常打交道的是csv文件和list,dict类型.涉及到的主要需求有: 将一个二重列表[[],[]]写入到csv文件中 从文本文件中读取返回为列表 将一字典写入到csv文件中 从csv ...

  9. window连接linux nfs服务器 —— 网络错误 53

    在度娘以"win7 nfs mount 网络错误 - 53"为关键字,翻了第一页,GG... 于是去找谷哥,第一页 [all variants] Get NFS working w ...

  10. 【Usaco2008 Mar】土地购买

    [题目描述] 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; ...