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.