淺析selenium的PageFactory模式


前面的文章介紹了selenium的PO模式,見文章:http://www.cnblogs.com/qiaoyeye/p/5220827.html。下面介紹一下PageFactory模式。

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);

后者加入了初始化元素時等待時間。

3.在代碼中如何設計

a.新建一個BasePage類

class BasePage {
    WebDriver dr;
    private final int TIMEOUT = 10;
    
    BasePage() {}
    
    BasePage(WebDriver dr) {
        this.dr = dr;
        PageFactory.initElements(new AjaxElementLocatorFactory(dr, TIMEOUT) , this);
    }
    
    BasePage(WebDriver dr, final String title) {
        this.dr = dr;
        
        //如果不進行判斷,
        WebDriverWait wait = new WebDriverWait(dr,TIMEOUT);
        try{
            boolean flag = wait.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是:" + dr.getTitle());
        }
        
        PageFactory.initElements(new AjaxElementLocatorFactory(dr, TIMEOUT) , this);
        
    }
    void fun2() {}
    void fun3() {}
}

b.建兩個頁面類LoginPage和HomePage,分別繼承BasePage

class LoginPage extends BasePage{
    @FindBy(id="UserName")
    @CacheLookup //加入緩存,更新值的時候先從緩存中取
    private WebElement input_username;
    
    @FindBy(css="html body div.loginbox div#NoCodeLogin.formbox.errorBox div#ElongLogin div#password_tip.inputbox.login_pw input.blur")
    @CacheLookup
    private WebElement temp_input_password;
    
    
    @FindBy(id="PassWord")
    @CacheLookup
    private WebElement input_password;
    //WebDriver dr;
    
    /*    
    @FindBy(name="captcha")
    @CacheLookup
    WebElement input_imgcode;
    
    @FindBy(css="html.show-app-promotion-bar.cssanimations.csstransforms.csstransitions.flexbox.no-touchevents.no-mobile body.zhi.no-auth div.index-main div.index-main-body div.desk-front.sign-flow.clearfix.sign-flow-simple div.view.view-signin form div.group-inputs div.input-wrapper.captcha-module div.captcha-container img.js-refresh-captcha.captcha")
    @CacheLookup
    WebElement imgcode;*/
    
    @FindBy(className="loginbtn")
    @CacheLookup
    private WebElement button_submit;
    
    LoginPage(WebDriver dr) {
        super(dr);
    }
    LoginPage(WebDriver dr, String titile) {
        super(dr, titile);
    }
    HomePage login() {

        typeusername("0000");
        typepassword("111111");
        button_submit.click();

        return new HomePage(dr,"藝龍機票】機票查詢,特價機票,打折飛機票-藝龍旅行網eLong.com");
    }
    
    void typeusername(String name) {
        input_username.clear();
        input_username.sendKeys(name);
    }
    void typepassword(String password) {
        temp_input_password.click();
        input_password.clear();
        input_password.sendKeys(password);
    }
    void formsubmit() {
        
    }
}

class HomePage extends BasePage {
    
    HomePage(WebDriver dr) {
        super(dr);
        // TODO Auto-generated constructor stub
    }

    public HomePage(WebDriver dr, String title) {
        // TODO Auto-generated constructor stub
        super(dr, title);
    }
    
    @FindBy(id="btnSearch")
    private WebElement btn_search;
    
}

c.建一個測試類,做為入口

package com.test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @author  qiaojiafei
 * @version 創建時間:2016年4月22日 下午1:26:59
 * 類說明
 */
public class TestFactory {
    public static void main(String args[]) {
        WebDriver dr = new SafariDriver();
        dr.get("https://secure.elong.com/passport/login_cn.html");
        String logintitle = "會員登錄–elong.com藝龍旅行網";
        LoginPage lp = new LoginPage(dr, logintitle);
        HomePage hp = lp.login();
    }
}

到底為止結束,在每個頁面的構造方法,加入頁面title的驗證,是為了保證跳轉的頁面與預期的頁面一致后,再進行頁面元素的初始化,否則,頁面都沒跳轉正確,就進行元素初始化時多余的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM