美文网首页自动化测试Selenium测试开发栈
WebDriver对象管理之PageObject与PageFac

WebDriver对象管理之PageObject与PageFac

作者: 测试开发栈 | 来源:发表于2017-06-08 09:13 被阅读674次

在之前的<a href="http://www.jianshu.com/p/71e04066be61">自动化框架搭建</a>文章中有提到过对象管理(或元素管理),而WebDriver给我们提供了两种对象管理思路PageObject与PageFactory,今天我就对比下两种方式的异同~


关于PageObject
将一个页面或一类元素看做是一个页面对象,一个页面对象对应一个类,将元素对象的获取定义在这一个类中。(解释总显得那么苍白,看代码吧)
方式一:
public class HomePage {
   protected WebDriver driver
   public HomePage(WebDriver driver) {
       this.driver= driver;
   }
   public WebElement getUserName() {
     By by = By.xpath("//span[@class='name']");
     return driver.findElement(by);
   }
   public List<WebElement> getLinks() {
     By by = By.xpath("//a");
     return driver.findElements(by);
   }
   public WebElement getHomeLink() {
     By by = By.xpath("//a[contains(text(),'首页')]");
     return driver.findElement(by);
   }
}

方式二:

public class HomePage {
   protected WebDriver driver
   public HomePage(WebDriver driver) {
     this.driver= driver;
   }
   public WebElement getUserName() {
     By by = By.xpath("//span[@class='name']");
     return isElementExist(by) ? driver.findElement(by) : null;
   }
   public List<WebElement> getLinks() {
     By by = By.xpath("//a");
     return isElementsExist(by) ? driver.findElement(by) : null;
   }
   public WebElement getHomeLink() {
     By by = By.xpath("//a[contains(text(),'首页')]");
     return isElementExist(by) ? driver.findElement(by) : null;
   }
}

方式一直接返回findElement()方法,遇到元素未定位到等情况会抛出NoSuchElementException 异常,从而中断测试,所以我这边给出了方式二(推荐方式二),这里给出isElementExist()方法:

public static boolean isElementExist(By selector){
     try {
       driver.findElement(selector); 
       return true;
     } catch (NoSuchElementException e) {
       return false;
     }
}

关于PageFactory
整体思想同PageObject,只是在表现形式上通过注解(@FindBy)的方式定义元素对象,看代码:****
方式一:

public class HomePage {
 @FindBy(xpath = "//span[@class='name']")
 @CacheLookup
 public static WebElement userName;
 
 @FindBy(xpath = "//a")
 @CacheLookup
 public static List<WebElement> links;
 
 @FindBy(xpath = "//a[contains(text(),'首页')]")
 @CacheLookup
 public static WebElement homeLink;
}

使用对象的时候需要先初始化类:

PageFactory.initElements(driver ,HomePage.class);
HomePage.homeLink.click();

方式二:

public class HomePage extends BasePage{
 public HomePage(WebDriver driver) {
        super(driver);
}
 public HomePage(WebDriver driver, String titile) {
        super(driver, titile);
  }
 @FindBy(xpath = "//span[@class='name']")
 @CacheLookup
 public WebElement userName;
 
 @FindBy(xpath = "//a")
 @CacheLookup
 public List<WebElement> links;
 
 @FindBy(xpath = "//a[contains(text(),'首页')]")
 @CacheLookup
 public WebElement homeLink;
}

public class BasePage {
 public static final String MAIN_TITLE = "XXX平台";
 public WebDriver driver;
 public String title;
        private final int TIMEOUT = 10;
    
        public BasePage() {}
    
        public BasePage(WebDriver driver) {
            this.driver = driver;
            PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT) , this);
        }
    
        public BasePage(WebDriver driver, final String title) {
            this.driver = driver;
            this.title = title;
            try{
         new WebDriverWait(driver,TIMEOUT).until(new ExpectedCondition<Boolean>(){
                @Override
                public Boolean apply(WebDriver arg0) {
                    // TODO Auto-generated method stub
                    String acttitle = arg0.getTitle();
                    return acttitle.equals(title);                    
                }});
        }catch(TimeoutException te) {
            throw new IllegalStateException("当前页面title(" + driver.getTitle() + ")与预期("+ title +")不一致");
        }
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT) , this);
    }
}

方式二(推荐使用)增加了页面和元素等待时间,比方式一更加健壮,使用对象时也更加清晰:

HomePage home = new HomePage(driver,"XXX");
home.homeLink.click();

总结
上面通过代码的形式给出了PageObject与PageFactory的区别,并且通过代码优化的方式给出了两种对象管理思路推荐的使用方式,两种方式我平时都有使用,有时候还混着用,总体来说并不能说谁好谁坏,各有千秋,看个人的习惯,在元素判断上我觉得PageObject方便一些,元素不存在就是null,这个思路很直接;PageFactory的好处除了使用注解使代码更整洁之外,还可以设置页面和元素加载等待时间,总体来说,还是推荐PageFactory。

原文来自下方公众号,转载请联系作者,并务必保留出处。
想第一时间看到更多原创技术好文和资料,请关注公众号:测试开发栈

相关文章

  • WebDriver对象管理之PageObject与PageFac

    在之前的 自动化框架搭建 文章中有提到过对象管理(或元素管理),而WebDriver给我们提供了两种对象管理思路P...

  • selenium 模拟登录豆瓣

    from seleniumimport webdriver #实例化一个对象 driver=webdriver.C...

  • 第三单元 PO模型

    一、PO模型介绍 在自动化中,Selenium自动化测试中有一个名字经常被提及PageObject(思想与面向对象...

  • PO模型

    一、PO模型介绍 在自动化中,Selenium自动化测试中有一个名字经常被提及PageObject(思想与面向对象...

  • PO模型

    一、PO模型介绍 在自动化中,Selenium自动化测试中有一个名字经常被提及PageObject(思想与面向对象...

  • PO设计模式

    1 PO设计模式 什么是设计模式? PO(PageObject)设计模式将某个页面的所有元素对象定位和对元素对象的...

  • PageObject分离E2E测试

    PageObject 老马关于PageObject的定义 It should allow a software c...

  • 【Selenium】对pageobject的理解

    pageobject是testNG测试框架里测试类的一种组合模式,把测试类里的对象和操作分离。

  • WebDriver之JsonWireProtocol

    WebDriver之JsonWireProtocol### JsonWireProtocol说明 JsonWir...

  • UI自动化(四)selenium浏览器操作

    一、创建webdriver对象并打开对应网页 from selenium import webdriverdriv...

网友评论

  • efb454031551:提供思路:public static By INT_PAGE=By.xpath("");
    之后统一在findElement(by)进行相关的判断
  • smmtest:PageObject 为什么要用方法返回,直接写成变量public Webelement element=driver.findelement(By.xpath());
    一行行的不是更简洁么?
    而且封装Webelement没有必要,封装By就可以了.你觉得呢?
    测试开发栈:如果使用上面文章中PageObject 的方式一,确实还不如用你这种简写方式,但是更推荐使用方式二,可以对NoSuchElementException 异常进行处理,判断对象时也方便一些。

本文标题:WebDriver对象管理之PageObject与PageFac

本文链接:https://www.haomeiwen.com/subject/eqezfxtx.html