zoukankan      html  css  js  c++  java
  • 浅析selenium的PageFactory模式 PageFactory初始化pageobject

    1.首先介绍FindBy类:

    For example, these two annotations point to the same element:

     @FindBy(id = "foobar") WebElement foobar;
     @FindBy(how = How.ID, using = "foobar") WebElement foobar; 

    and these two annotations point to the same list of elements:

     @FindBy(tagName = "a") List<WebElement> links;
     @FindBy(how = How.TAG_NAME, using = "a") List<WebElement> links;

    用来分别查找单个元素和多个元素的两种用法,支持的类型有:classNamecssidlinkTextnamepartialLinkTexttagNamexpath

    How支持的类型和上面差不多。

    2.接着介绍PageFactory类

    Factory class to make using Page Objects simpler and easier.

    它提供的方法都是静态的,可以直接调用,我们在用完findby后,需要进行元素初始化,则需要调用下面的方法

    initElements(ElementLocatorFactory factory, java.lang.Object page)、initElements(FieldDecorator decorator, java.lang.Object page)、initElements(WebDriver driver, java.lang.Class<T> pageClassToProxy)、initElements(WebDriver driver, java.lang.Object page)

    我们在实际使用中可以这样用:

    PageFactory.initElements(dr, XXX.class);

    或者

    PageFactory.initElements(new AjaxElementLocatorFactory(dr, 10) ,XXX.class);

    后者加入了初始化元素时等待时间。

    PageFactory是为了支持页面设计模式而开发出来的,它的方法在selenium.support库里面。

    PageFactory它提供初始化页面元素的方法,如果页面存在大量的AJAX的技术,只要页面更新一次,它就好重新查找一次元素,所以不会出现StaleElementException这个error,

    如果你不想要它每次都更新,你可以加上@CacheLookup.

    页面设计模式,可以提供你一个接口,然后你在这个接口上面,构建你自己项目中的页面对象,使用PageFactory使得测试更简单,更少的代码编写。

    如果@FindBy没有指定,它会默认的查找id的属性,然后是name属性,如果还没有找到就会报错。 如果这个元素存在,我们不用担心它, pageFactory会初始化这个元素,不会报任何错误。

    先看个列子:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    public class BaiduSearchPage {   WebDriver driver;   @FindBy(id = "kw")   @CacheLookup   WebElement searchField;   @FindBy(id = "su")   @CacheLookup   WebElement searchButton;   public BaiduSearchPage(WebDriver driver){     this.driver = driver;     PageFactory.initElements(driver,this);   }   public void inputText(String search){     searchField.sendKeys(search);   }   public void clickButton(){     searchButton.click();   } } import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;
    public class Test { public static void main(String[] args) {   BaiduSearchPage searchPage;
      WebDriver driver
    =new FirefoxDriver();   driver.manage().window().maximize();   driver.get("http://www.baidu.com");   searchPage =new BaiduSearchPage(driver);   searchPage.inputText("selenium");   searchPage.clickButton();   } }

    我们平时写查找元素,喜欢倾向于driver.findElement(by)这种方法,但是有的时候我们需要考虑查找失败,或者AJAX的情况,但是pageFactory就不需要,这使得查找页面元素更简单,快速。

    基于页面设计对象, 我们可以编写更少的代码去完成更多的测试案例。

  • 相关阅读:
    IPC之——消息队列
    特殊命令
    面试概念集锦
    守护进程(精灵进程)
    IP SSL HTTPS
    钉钉监控样例
    中间人攻击
    iptables firewalld
    简单暴力高效率的OSM全球地图
    解决ubuntu使用命令sudo apt -get install 安装东西时出现"E: Sub-process /usr/bin/dpkg returned an error code (1) "的错误 问题描述:
  • 原文地址:https://www.cnblogs.com/111testing/p/7104241.html
Copyright © 2011-2022 走看看