selenium2.0的初步封装(java版本)

来源:互联网 发布:垃圾桶 知乎 编辑:程序博客网 时间:2024/06/11 21:30

我们都知道, 在本地创建java项目后,引入selenium-java-2.35.0.jar   selenium-support-2.35.0.jar junit-4.8.1.jar等等jar包之后,(或者创建Java maven项目,在pom.xml的<dependency></dependency>中添加依附, Maven可以或许主动响应版本的jar包), 之后, 就可以在本地开辟selenium主动化脚本


       在完全没有封装的景象下, 一般的次序是:


       1)获得浏览器驱动: 


        WebDriver driver=new FirefoxDriver();


       2)经由过程WebDriver供给的办法打开网页:


       driver.get("urlString");  或者  driver.navigate.to("urlString")


       3)经由过程driver.findElement(By.***)找到元素


        找元素的过程, 须要做守候处理惩罚, 可以用Thread.sleep(), 然而为了进步脚本的效力和结实性, 一般不采取这种守候体式格式,可以经由过程采取 WebDriverWait类+ExpectedConditions接口体式格式来灵活守候元素呈现,具体处理惩罚可以见“selenium1.0和selenium2.0页面守候处理惩罚详解”


       4)对元素进行操纵


        上方步调3获得元素WebElement,经由过程WebElement供给的办法, 对元素进行操纵(click()/sendKeys()/submit()/getText())等等


       5)经由过程打开页面、找元素、守候元素、对元素操纵等一系列模仿工钱动作后,第5步就须要做真正要测试的目标,即搜检操纵后的成果


       比如, 验证某个元素是否呈现, 验证是否输出正确的成果, 此时的步调可所以试图去找元素,待 wait.until()抛异常后, 还是没找到元素的话, 断定为该元素不存在。 在验证输出成果时,反复上述3) 4) 5)步调取出元素, 再经由过程if  String.contains()  或者String.equals()来断定输出成果


 


      显然, 上述是斗劲繁琐的过程, 写出来的脚本, 也会呈现很多冗余的雷同操纵代码,如许的脚本,保护起来吃力, 下面,将对上方的过程做一个小小封装, 如许可以只调用一个办法, 就可以完成 “查找/守候/操纵元素” 、 “查找/守候/断定元素是否存在” 或者  “查找/守候/断定输出内容” 等等


1、


设定容许最长的守候时候:


WebDriverWait wait=new WebDriverWait(driver, 10);


2、查找元素是否存在的过程,放在一个类里面



class ElementExistOrNot implements Function<WebDriver, Boolean>{//第一个参数为apply办法的参数类型,第二个参数为apply办法的返回类型
private By by;
private String sign;

public ElementExistOrNot(By by, String sign) {
this.by = by;
this.sign = sign;
}
@Override
public Boolean apply(WebDriver driver) {
try{
WebElement e=driver.findElement(by);
if(sign.equals("yes")){
return true;
}else{
return false;
}
}catch (Exception e){
if(sign.equals("yes")){
return false;
}else{
return true;
}
}
}
}


3、进行简单封装成办法供调用



//完成对元素的 ”查找+守候+验证存在性“ 的封装
public void waitUntilElementExist(By by){
ElementExistOrNot isTrue=new ElementExistOrNot(by, "yes");
try{
wait.until(isTrue);
}catch (Exception e){
fail("Element [" + by + "] should presents!");
}
}


4、有了这些办法后,再持续这些办法地点的类, 就可以轻松调用啦~


 


PS:


别的,还有其他一些 “查找/守候/操纵元素” 、  “查找/守候/断定输出内容” 等等,思路基****同, 下面再供给一些封装体式格式的完全源码:



package com.jennifer.functiontest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;

public class jenniferBase extends Assert {
protected WebDriver driver;
protected WebDriverWait wait;

@Before
public void init(){
driver=new FirefoxDriver();
wait=new WebDriverWait(driver, 10);
}

//获得带有守候机制的WebElement(对“找元素+守候”的封装)
public WebElement e(By by){
try{
waitUntilElementExist(by);
return driver.findElement(by);
}catch (Exception e) {
fail("Can""t find element: ["+by+"]");
}
return null;
}
//完成对元素的“查找+守候(直到元素存在)+验证+点击 操纵”的封装
public void clickAndWait(By by){
e(by).click();
}

//完成对元素的 ”查找+守候+验证存在性“ 的封装
public void waitUntilElementExist(By by){
ElementExistOrNot isTrue=new ElementExistOrNot(by, "yes");
try{
wait.until(isTrue);
}catch (Exception e){
fail("Element [" + by + "] should presents!");
}
}
//完成对元素的“查找+守候+验证不存在性”的封装
public void waitUntilElementNotExist(By by){
ElementExistOrNot isTrue=new ElementExistOrNot(by, "no");
try{
wait.until(isTrue);
}catch (Exception e) {
fail("Element [" + by + "] should not presents!");
}
}

//完成对元素的“查找+守候+验证其text存在性”的封装
public void waitUntilElementEqualsText(By by,String s){
waitUntilElementExist(by);
ElementEqualsOrNot isTrue=new ElementEqualsOrNot(by, "yes", s, "text");
try{
wait.until(isTrue);
}catch (Exception e) {
fail("Element ["+by+"] text should equals"+s+"but not it equals"+e(by).getText());
}
}
//完成对元素的“查找+守候+验证其value存在性”的封装
public void waitUntilElementEqualsValue(By by,String s){
waitUntilElementExist(by);
ElementEqualsOrNot isTrue=new ElementEqualsOrNot(by, "yes", s, "value");
try{
wait.until(isTrue);
}catch (Exception e) {
fail("Element ["+by+"] value should equals:"+s+"but not it equals:"+e(by).getValue());
}
}

@After
public void tearDown(){
driver.quit();
}
}

class ElementExistOrNot implements Function<WebDriver, Boolean>{//第一个参数为apply办法的参数类型,第二个参数为apply办法的返回类型
private By by;
private String sign;

public ElementExistOrNot(By by, String sign) {
this.by = by;
this.sign = sign;
}
@Override
public Boolean apply(WebDriver driver) {
try{
WebElement e=driver.findElement(by);
if(sign.equals("yes")){
return true;
}else{
return false;
}
}catch (Exception e){
if(sign.equals("yes")){
return false;
}else{
return true;
}
}
}
}
class ElementEqualsOrNot implements Function<WebDriver,Boolean>{
private By by;
private String sign;
private String s;
private String textOrValue;

public ElementEqualsOrNot(By by, String sign, String s, String textOrValue) {
this.by = by;
this.sign = sign;
this.s = s;
this.textOrValue = textOrValue;
}

@Override
public Boolean apply(WebDriver driver) {
WebElement e=driver.findElement(by);
if(sign.equals("yes")){ //断定存在性
if(textOrValue.equals("text")){ //断定text的存在性
return e.getText().equals(s);
}else{ //断定value的存在性
return e.getValue().equals(s);
}
}else{//断定不存在性
if(textOrValue.equals("text")){
return !e.getText().equals(s);
}else{
return !e.getValue().equals(s);
}
}

}
}



 


以上只是小我的一点封装思路,其他有此外更好封装规划, 请指教^_^

0 0