zoukankan      html  css  js  c++  java
  • 浅析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的验证,是为了保证跳转的页面与预期的页面一致后,再进行页面元素的初始化,否则,页面都没跳转正确,就进行元素初始化时多余的。

     

  • 相关阅读:
    李超线段树 [Heoi2013]Segment
    [置顶] 九月半集训总结
    [置顶] 我想学学
    图论+前缀和 任(duty)
    模拟 飞(fly)
    入坑 可持久化线段树——主席树
    一次爆炸的联考
    HASH+平衡树 [JSOI2008]火星人prefix
    乱搞+STL平衡树 序列
    数学+图论 建造游乐场
  • 原文地址:https://www.cnblogs.com/qiaoyeye/p/5422210.html
Copyright © 2011-2022 走看看