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

       我们都知道, 在本地创建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);
            }
        }
        
    }
}


 

以上只是个人的一点封装思路,其他有别的更好封装方案, 请指教^_^

 

    

      

       

       

      

  

posted @ 2013-12-09 13:42  jenniferhuang  阅读(3166)  评论(0编辑  收藏  举报