Analysis on the pagefactory mode of selenium

Source: Internet
Author: User
Tags tagname

The previous article describes selenium's PO mode, see article: http://www.cnblogs.com/qiaoyeye/p/5220827.html. The pagefactory mode is described below.

1. First introduce the FindBy class:

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

And these annotations point to the same list of elements:

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

Used to find two usages of a single element and multiple elements, the supported types are:className、css、id、linkText、name、partialLinkText、tagName、xpath。

How supports the same type as above.

2. Then introduce the Pagefactory class

Factory class to make using Page Objects simpler and easier.

< Span class= "Membernamelink" > The methods it provides are static and can be called directly, we need to initialize the element after we run out of findby, we need to call the following method

< Span class= "Membernamelink" > 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)

We can use it in practice:

Pagefactory.initelements (DR, XXX.  Class);

Or

Pagefactory.initelements (new ajaxelementlocatorfactory (DR, Ten), XXX.  Class);

The latter joins the wait time when initializing the element.

3. How to Design in code

A. Create a new BasePage class

classbasepage {Webdriver dr; Private Final intTIMEOUT = 10; BasePage () {} basepage (Webdriver dr) { This. Dr =Dr; Pagefactory.initelements (NewAjaxelementlocatorfactory (DR, TIMEOUT), This); } basepage (Webdriver Dr,FinalString title) {         This. Dr =Dr; //If you do not make a judgment,webdriverwait wait =Newwebdriverwait (dr,timeout); Try{            BooleanFlag = Wait.until (NewExpectedcondition<boolean>() {@Override PublicBoolean Apply (Webdriver arg0) {//TODO auto-generated Method StubString Acttitle =Arg0.gettitle (); returnActtitle.equals (title);        }}); }Catch(TimeoutException te) {Throw NewIllegalStateException ("Not currently expected page, the current page title is:" +dr.gettitle ()); } pagefactory.initelements (NewAjaxelementlocatorfactory (DR, TIMEOUT), This); }    voidfun2 () {}voidfun3 () {}}

B. Build two page Classes LoginPage and homepage, each inheriting basepage

classLoginPageextendsbasepage{@FindBy (ID= "UserName") @CacheLookup//Add the cache and update the value by first taking the cache    Privatewebelement Input_username; @FindBy (CSS= "HTML body div.loginbox div#nocodelogin.formbox.errorbox div#elonglogin DIV#PASSWORD_TIP.INPUTBOX.LOGIN_PW Input.blur ") @CacheLookupPrivatewebelement Temp_input_password; @FindBy (ID= "PassWord") @CacheLookupPrivatewebelement 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") @CacheLookupPrivatewebelement 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 NewHomepage (dr, "Yi Long Ticket" ticket, special airfares, discounted air tickets-yi Long Travel Network elong.com "); }        voidtypeusername (String name) {input_username.clear ();    Input_username.sendkeys (name); }    voidTypepassword (String password) {temp_input_password.click ();        Input_password.clear ();    Input_password.sendkeys (password); }    voidFormsubmit () {}}classHomepageextendsbasepage {homepage (Webdriver dr) {Super(DR); //TODO auto-generated Constructor stub    }     Publichomepage (Webdriver Dr, String title) {//TODO auto-generated Constructor stub        Super(DR, title); } @FindBy (ID= "Btnsearch")    Privatewebelement Btn_search; }

C. Build a test class as a portal

 Packagecom.test;ImportJava.util.concurrent.TimeUnit;Importorg.openqa.selenium.TimeoutException;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.safari.SafariDriver;ImportOrg.openqa.selenium.support.CacheLookup;Importorg.openqa.selenium.support.FindBy;Importorg.openqa.selenium.support.PageFactory;Importorg.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;Importorg.openqa.selenium.support.ui.ExpectedCondition;Importorg.openqa.selenium.support.ui.ExpectedConditions;Importorg.openqa.selenium.support.ui.WebDriverWait;/** * @authorQiaojiafei *@versioncreated: April 22, 2016 1:26:59 * class Description*/ Public classTestfactory { Public Static voidMain (String args[]) {Webdriver Dr=NewSafaridriver (); Dr.get ("Https://secure.elong.com/passport/login_cn.html"); String Logintitle= "Member Login –elong.com Yi Long Travel Network"; LoginPage LP=NewLoginPage (DR, Logintitle); Homepage HP=Lp.login (); }}

In the end, in each page of the construction method, add the page title of the validation, is to ensure that the page with the expected page to be consistent, and then the page element initialization, otherwise, the page does not jump correctly, the element initialization is unnecessary.

Analysis on the pagefactory mode of selenium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.