Selenium webdriver Java 封装与重用
目的
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 Java 封装与重用的更多相关文章
- Selenium Webdriver java 积累一
Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...
- [selenium webdriver Java]常用api
1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...
- [selenium webdriver Java]元素定位——findElement/findElements
策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver ...
- [selenium webdriver Java]检查元素是否存在
Selenium WebDriver没有实现Selenium RC的isElementPresent()方法来检查页面上的元素是否存在. 在WebDriver中封装一个类似的方法,如下: public ...
- [selenium webdriver Java]隐式的等待同步
Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...
- Selenium WebDriver java 简单实例
开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...
- Java 学习笔记 (二) Selenium WebDriver Java 弹出框
下面这段实例实现了以下功能: 1. profile使用用户本地电脑上的 (selenium 3有问题.因为selenium 3把profile复制到一个temp文件夹里,但并不复制回去.所以每次打开仍 ...
- Selenium webdriver Java 操作IE浏览器
V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...
- Selenium webdriver Java firefox 路径设置问题
问题: Cannot find firefox binary in PATH. Make sure firefox is installed. 原因:selenium找不到Firefox浏览器. 方法 ...
随机推荐
- [ python3 ] 基于zabbix 自动抓取每天监控数据
通过python登录到zabbix直接抓取每天的数据的图片趋势图,并制作成静态index.html给与展示并发送提示邮件. 操作系统:Centos6.7 python版本:python3.5 #!/u ...
- 【 总结 】linux中test命令详解
test命令在bash shell脚本中经常以中括号([])的形式出现,而且在脚本中使用字母来表示比符号表示更专业,出错率更低. 测试标志 代表意义 文件名.文件类型 -e 该文件名是否存在 -f 该 ...
- 详解nginx、php-fpm和mysql用户权限
通常情况下,我们运行web应用的服务器有CentOS.Ubuntu.Debian等等的Linux发行版本.这时候,构成服务架构所必须的Nginx.php和MySQL等应用的权限控制就显得非常重要,各个 ...
- Go简单的Goroutine示例
最简单的,接下来,会是竞争,加锁... package main import ( "fmt" "runtime" "sync" ) var ...
- ionic3 cordova ionic-native插件
ionic-native插件 cordova安装插件 以及 ionic-native插件使用过程以及步骤 cordova plugin add cordova-plugin-插件名称. //安装插件 ...
- [linux]多线程下载
axel -n 10 -o /tmp/ http://soft.vpser.net/lnmp/lnmp0.7-full.tar.gz # 10是线程数
- [转]认识session
今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...
- circusctl命令在ubuntu 上执行,卡住的现象处理。
1. circus介绍 circus是一个进程管理工具,类似于supervisod. 2. circusctl是circusd进程的管理工具 3. circus的安装 pip3 install cir ...
- 转 select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...
- Codeforces 915 G Coprime Arrays
Discipntion Let's call an array a of size n coprime iff gcd(a1, a2, ..., an) = 1, where gcd is the g ...