The PageFactory

The PageFactory

原文地址:https://github.com/SeleniumHQ/selenium/wiki/PageFactory

In order to support the PageObject pattern, WebDriver's support library contains a factory class.//PageFactory是用来支持PageObject的。

好处一:自己决定是否缓存找到的元素。

好处二:只有用到页面上的一个元素时,才会去定位这个元素。

A Simple Example

In order to use the PageFactory, first declare some fields on a PageObject that are WebElements or List<WebElement>, for example://基于PageObject声明field和相关的方法。这里的field是一些元素变量。

 1 package org.openqa.selenium.example;
 2 
 3 import org.openqa.selenium.WebElement;
 4 
 5 public class GoogleSearchPage {
 6     // Here's the element
 7     private WebElement q;
 8 
 9     public void searchFor(String text) {
10         // And here we use it. Note that it looks like we've
11         // not properly instantiated it yet....
12         q.sendKeys(text);
13         q.submit();
14     }
15 }

In order for this code to work and not throw a NullPointerException because the "q" field isn't instantiated, we need to initialise the PageObject://初始化。

 1 package org.openqa.selenium.example;
 2 
 3 import org.openqa.selenium.WebDriver;
 4 import org.openqa.selenium.WebElement;
 5 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 6 import org.openqa.selenium.support.PageFactory;
 7 
 8 public class UsingGoogleSearchPage {
 9     public static void main(String[] args) {
10         // Create a new instance of a driver
11         WebDriver driver = new HtmlUnitDriver();
12 
13         // Navigate to the right place
14         driver.get("http://www.google.com/");
15 
16         // Create a new instance of the search page class
17         // and initialise any WebElement fields in it.
18         GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);
19 
20         // And now do the search.
21         page.searchFor("Cheese");
22     }
23 } 

Explanation

The PageFactory relies on using sensible defaults: the name of the field in the Java class is assumed to be the "id" or "name" of the element on the HTML page. That is, in the example above, the line://对上面样例程序的解释:声明field时,使用元素的id或者name。

1     q.sendKeys(text);

is equivalent to:

1     driver.findElement(By.id("q")).sendKeys(text);

The driver instance that's used is the one that's passed to the PageFactory's initElements method.

In the example given, we rely on the PageFactory to instantiate the instance of the PageObject. It does this by first looking for a constructor that takes "WebDriver" as its sole argument (public SomePage(WebDriver driver) {). If this is not present, then the default constructor is called. Sometimes, however, the PageObject depends on more than just an instance of the WebDriver interface. Should this be the case, it is possible to get the PageFactory to initialise the elements of an already constructed object://初始化时,可以使用xxxPage.class的一个实例。

1 ComplexPageObject page = new ComplexPageObject("expected title", driver);
2 
3 // Note, we still need to pass in an instance of driver for the 
4 // initialised elements to use
5 PageFactory.initElements(driver, page);

Making the Example Work Using Annotations

When we run the example, the PageFactory will search for an element on the page that matches the field name of the WebElement in the class. It does this by first looking for an element with a matching ID attribute. If this fails, the PageFactory falls back to searching for an element by the value of its "name" attribute.

Although the code works, someone who's not familiar with the source of the Google home page may not know that the name of the field is "q". Fortunately, we can pick a meaningful name and change the strategy used to look the element up using an annotation://使用注解,给元素变量取一个有意义的名字。

 1 package org.openqa.selenium.example;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.support.FindBy;
 5 import org.openqa.selenium.support.How;
 6 import org.openqa.selenium.WebElement;
 7 
 8 public class GoogleSearchPage {
 9     // The element is now looked up using the name attribute
10     @FindBy(how = How.NAME, using = "q")
11     private WebElement searchBox;
12 
13     public void searchFor(String text) {
14         // We continue using the element just as before
15         searchBox.sendKeys(text);
16         searchBox.submit();
17     }
18 } 

One wrinkle that remains is that every time we call a method on the WebElement, the driver will go and find it on the current page again. In an AJAX-heavy application this is what you would like to happen, but in the case of the Google search page we know that the element is always going to be there and won't change. We also know that we won't be navigating away from the page and returning (which would mean that a different element with the same name would be present) It would be handy if we could "cache" the element once we'd looked it up://使用PageFactory,有一个好处是,自己决定是否缓存找到的元素。默认情况下,每次调用方法时,都会重新去定位页面元素。这对于处理使用AJAX技术的页面很有用。处理其它的页面,可以找到元素后就缓存起来。

 1 package org.openqa.selenium.example;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.support.CacheLookup;
 5 import org.openqa.selenium.support.FindBy;
 6 import org.openqa.selenium.support.How;
 7 import org.openqa.selenium.WebElement;
 8 
 9 public class GoogleSearchPage {
10     // The element is now looked up using the name attribute,
11     // and we never look it up once it has been used the first time
12     @FindBy(how = How.NAME, using = "q")
13     @CacheLookup
14     private WebElement searchBox;
15 
16     public void searchFor(String text) {
17         // We continue using the element just as before
18         searchBox.sendKeys(text);
19         searchBox.submit();
20     }
21 } 

Reducing Verbosity

The example above is still a little verbose. A slightly cleaner way of annotating the field would be://简化注解

1 public class GoogleSearchPage {
2   @FindBy(name = "q")
3   private WebElement searchBox;
4 
5   // The rest of the class is unchanged.
6 }

Notes

  • If you use the PageFactory, you can assume that the fields are initialised. If you don't use the PageFactory, then NullPointerExceptions will be thrown if you make the assumption that the fields are already initialised.//使用PageFactory时,不要忘记初始化。
  • List<WebElement> fields are decorated if and only if they have @FindBy or @FindBys annotation. Default search strategy "by id or name" that works for WebElement fields is hardly suitable for lists because it is rare to have several elements with the same id or name on a page.//List<WebElement>使用@FindBy 或 @FindBys注解。
  • WebElements are evaluated lazily. That is, if you never use a WebElement field in a PageObject, there will never be a call to "findElement" for it.//只有在用到页面上的一个元素时,才会去定位这个元素。
  • The functionality works using dynamic proxies. This means that you shouldn't expect a WebElement to be a particular subclass, even if you know the type of the driver. For example, if you are using the HtmlUnitDriver, you shouldn't expect the WebElement field to be initialised with an instance of HtmlUnitWebElement.//要使用driver初始化,不要用webelement初始化。
原文地址:https://www.cnblogs.com/superbaby11/p/6093640.html