@FindBy @cachelookup, Pagefactory notes

Source: Internet
Author: User
first, @FindBy and @cachelookup usage

Code instance

Package page;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.support.CacheLookup;
Import Org.openqa.selenium.support.FindBy;
Import org.openqa.selenium.support.PageFactory;
public class Bdpage {
    @FindBy (id= "kw")
    @CacheLookup the public
    webelement keyword_input;
    @FindBy (id= "su")
    @CacheLookup public
    webelement Search_button;
    Public bdpage (Webdriver driver) {
        pagefactory.initelements (driver, this); 
    }   
}

The notation of the element declaration, these three lines are a whole:

@FindBy (id= "kw")
@CacheLookup public
webelement keyword_input;

Annotations:
@FindBy: This defines how the element you are looking for is positioned, such as the ID I used in the picture, then written @FindBy (id= "kw"), there are several other ways: @FindBy (name= "xx"), @FindBy (classname= " XX "), @FindBy (xpath=" xxx "), @FindBy (css=" xxx "), etc.

@CacheLookup: This means that the elements will be cached after the element is found, and repeated use of these elements will make the test much faster.

Webelement keyword_input: Is the variable name two, pagefactory

    Public bdpage (Webdriver driver) {
        pagefactory.initelements (driver, this); 
    }

Pagefactory is developed to support page design patterns, and its approach is in the Selenium.support library.
It provides a way to initialize page elements, if the page has a lot of Ajax technology, as long as the page updates once, it is good to re-find the element, so there is no staleelementexception this error,
Page design mode, you can provide an interface, and then you on this interface, build your own project Page object, using Pagefactory to make testing easier, less code writing.
If @findby is not specified, it defaults to the property of the lookup ID, then the Name property, and if it is not found, an error is given. If this element exists, we don't have to worry about it, pagefactory initializes the element and does not report any errors.

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.