目的

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. ExecuteReader执行查询实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. js动态添加table 数据tr td

    成果库修改:      要求主题列表随成果类型改变而改变      网上查询资料后开工,在成果类型下拉框添加change()事件触发Dwr,查询主题集合——动态创建/编辑Table      概要代码 ...

  3. Linux SSH: key, agent, keychain

    以前遇到过一个问题,在用有些 Linux 发行版时,用 ssh-keygen 产生好了密钥对并上传到了目标服务器,但每次登录都要重新输入. 这与 ssh-agent 有关,看如下 man ssh-ag ...

  4. birt报表中使用多个数据集。

    这个问题困扰了几天,也没搜到答案,由于工作需要,创建了两个数据集和两个表格,第一个数据集和表格之间没有任何问题.但是第二个数据集拖过去就显示不可用,除非拖到表格外面,当然也就没用了.一朋友说拖一个网格 ...

  5. Oracle解析 xml 记录一下(未完待续)

    Oracle解析 xml 记录一下. SQL> desc xmlparser; PROCEDURE FREEPARSER Argument Name                  Type  ...

  6. [转]Java远程方法调用

    Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...

  7. 用Guava辅助Throwable异常处理

    Guava的 Throwables 工具常常可以让exception处理更方便. Propagation 有时候,你会想把捕获的exception抛到上一个try/catch块.对于 RuntimeE ...

  8. 02_ Windows与Linux双重引导

    1. Grub2引导window. ---------------------步骤1--------------------------------- vim /etc/grub.d/40_custo ...

  9. 初涉JavaScript模式 (10) : 函数 【进阶用法】

    写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...

  10. Extjs之遍历Store内的数据

    Store作为数据的载体,通过下面的方法可以获得Store内的数据; Ext.define('haomlGeimjTongjGrid_store_data', { extend: 'Ext.data. ...