转:Selenium Webdriver使用技巧(四)
2016-03-29 00:11
281 查看
K 封装与重用
WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:
// Find the text inputelement by itsname
WebElement element =driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
那么,在测试用例可以这样简化调用:
sendKeys(By.name("q"),”Cheese!”);
看,这就简洁多了。
类似的封装还有:
package com.drutt.mm.end2end.actions;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.openqa.selenium.support.ui.WebDriverWait;
importcom.drutt.mm.end2end.data.TestConstant;
public class WebDriverAction {
//protected WebDriverdriver;
protectedRemoteWebDriverdriver;
protectedWebDriverWaitdriverWait;
protected booleanisWebElementExist(Byselector) {
try {
driver.findElement(selector);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
protectedStringgetWebText(By by) {
try {
return driver.findElement(by).getText();
} catch (NoSuchElementException e) {
return "Textnot existed!";
}
}
protectedvoidclickElementContainingText(By by, String text){
List<WebElement>elementList = driver.findElements(by);
for(WebElement e:elementList){
if(e.getText().contains(text)){
e.click();
break;
}
}
}
protectedStringgetLinkUrlContainingText(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 voidclick(Byby){
driver.findElement(by).click();
driver.manage().timeouts().implicitlyWait(TestConstant.WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
}
protectedStringgetLinkUrl(By by){
return driver.findElement(by).getAttribute("href");
}
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
小结:
按照上面的例子你可以对各个方法进行封装,使自己的代码更加简洁!
L 在selenium2.0中使用selenium1.0的API
Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium
RC API那么方便。Selenium2.0提供了使用Selenium RC API的方法:
//我用火狐浏览器作为例子
WebDriver driver = newFirefoxDriver();
String baseUrl="http://www.google.com";
Selenium selenium = newWebDriverBackedSelenium(driver, baseUrl);
//执行selenium命令
selenium.open("http://www.google.com");
selenium.type("name=q","cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();
selenium.stop();
我分别使用WebDriver API和SeleniumRC API写了一个Login的脚本,很明显,后者的操作更加简单明了。
WebDriver API写的Login脚本:
public void login() {
driver.switchTo().defaultContent();
driver.switchTo().frame("mainFrame");
WebElement eUsername= waitFindElement(By.id("username"));
eUsername.sendKeys(manager@ericsson.com);
WebElement ePassword= waitFindElement(By.id("password"));
ePassword.sendKeys(manager);
WebElementeLoginButton = waitFindElement(By.id("loginButton"));
eLoginButton.click();
}
SeleniumRC API写的Login脚本:
public void login() {
selenium.selectFrame("relative=top");
selenium.selectFrame("mainFrame");
selenium.type("username","manager@ericsson.com");
selenium.type("password","manager");
selenium.click("loginButton");
}
WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:
// Find the text inputelement by itsname
WebElement element =driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
那么,在测试用例可以这样简化调用:
sendKeys(By.name("q"),”Cheese!”);
看,这就简洁多了。
类似的封装还有:
package com.drutt.mm.end2end.actions;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.openqa.selenium.support.ui.WebDriverWait;
importcom.drutt.mm.end2end.data.TestConstant;
public class WebDriverAction {
//protected WebDriverdriver;
protectedRemoteWebDriverdriver;
protectedWebDriverWaitdriverWait;
protected booleanisWebElementExist(Byselector) {
try {
driver.findElement(selector);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
protectedStringgetWebText(By by) {
try {
return driver.findElement(by).getText();
} catch (NoSuchElementException e) {
return "Textnot existed!";
}
}
protectedvoidclickElementContainingText(By by, String text){
List<WebElement>elementList = driver.findElements(by);
for(WebElement e:elementList){
if(e.getText().contains(text)){
e.click();
break;
}
}
}
protectedStringgetLinkUrlContainingText(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 voidclick(Byby){
driver.findElement(by).click();
driver.manage().timeouts().implicitlyWait(TestConstant.WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
}
protectedStringgetLinkUrl(By by){
return driver.findElement(by).getAttribute("href");
}
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
小结:
按照上面的例子你可以对各个方法进行封装,使自己的代码更加简洁!
L 在selenium2.0中使用selenium1.0的API
Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium
RC API那么方便。Selenium2.0提供了使用Selenium RC API的方法:
//我用火狐浏览器作为例子
WebDriver driver = newFirefoxDriver();
String baseUrl="http://www.google.com";
Selenium selenium = newWebDriverBackedSelenium(driver, baseUrl);
//执行selenium命令
selenium.open("http://www.google.com");
selenium.type("name=q","cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();
selenium.stop();
我分别使用WebDriver API和SeleniumRC API写了一个Login的脚本,很明显,后者的操作更加简单明了。
WebDriver API写的Login脚本:
public void login() {
driver.switchTo().defaultContent();
driver.switchTo().frame("mainFrame");
WebElement eUsername= waitFindElement(By.id("username"));
eUsername.sendKeys(manager@ericsson.com);
WebElement ePassword= waitFindElement(By.id("password"));
ePassword.sendKeys(manager);
WebElementeLoginButton = waitFindElement(By.id("loginButton"));
eLoginButton.click();
}
SeleniumRC API写的Login脚本:
public void login() {
selenium.selectFrame("relative=top");
selenium.selectFrame("mainFrame");
selenium.type("username","manager@ericsson.com");
selenium.type("password","manager");
selenium.click("loginButton");
}
相关文章推荐
- CAT监控平台之装配篇
- 在 Django/Flask 开发服务器上使用 HTTPS
- 关系数据库基本概念。
- cv数据集及工具箱
- MySQL索引类型总结和使用技巧以及注意事项
- [MIREX] MIREX评测介绍
- Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.1:jar (attach-javadocs) on
- selenium技巧3
- NATS服务器部署及测试
- firefox中 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因
- 循环
- 多线程学习1
- JavaScript API 设计原则
- Java并发:终止线程和关闭线程池
- java-socket多线程网络编程案列--搭建本地简易聊天对话服务器
- LeetCode : First Missing Positive [java]
- 慕课网【PHP】3.27前所学学习内容纪要(1)
- float-浮动去空格
- mysql安装到最后一步无响应的问题解决
- iOS:代理(delegate)理解及使用