Class PageFactory


  • public class PageFactory
    extends java.lang.Object
    Factory class to make using Page Objects simpler and easier.
    See Also:
    Page Objects Wiki
    • Constructor Summary

      Constructors 
      Constructor Description
      PageFactory()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void initElements​(ElementLocatorFactory factory, java.lang.Object page)
      Similar to the other "initElements" methods, but takes an ElementLocatorFactory which is used for providing the mechanism for finding elements.
      static void initElements​(FieldDecorator decorator, java.lang.Object page)
      Similar to the other "initElements" methods, but takes an FieldDecorator which is used for decorating each of the fields.
      static <T> T initElements​(org.openqa.selenium.WebDriver driver, java.lang.Class<T> pageClassToProxy)
      Instantiate an instance of the given class, and set a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared, assuming that the field name is also the HTML element's "id" or "name".
      static void initElements​(org.openqa.selenium.WebDriver driver, java.lang.Object page)
      As initElements(WebDriver, Class) but will only replace the fields of an already instantiated Page Object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PageFactory

        public PageFactory()
    • Method Detail

      • initElements

        public static <T> T initElements​(org.openqa.selenium.WebDriver driver,
                                         java.lang.Class<T> pageClassToProxy)
        Instantiate an instance of the given class, and set a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared, assuming that the field name is also the HTML element's "id" or "name". This means that for the class: public class Page { private WebElement submit; } there will be an element that can be located using the xpath expression "//*[@id='submit']" or "//*[@name='submit']" By default, the element or the list is looked up each and every time a method is called upon it. To change this behaviour, simply annotate the field with the CacheLookup. To change how the element is located, use the FindBy annotation. This method will attempt to instantiate the class given to it, preferably using a constructor which takes a WebDriver instance as its only argument or falling back on a no-arg constructor. An exception will be thrown if the class cannot be instantiated.
        Type Parameters:
        T - Class of the PageObject
        Parameters:
        driver - The driver that will be used to look up the elements
        pageClassToProxy - A class which will be initialised.
        Returns:
        An instantiated instance of the class with WebElement and List<WebElement> fields proxied
        See Also:
        FindBy, CacheLookup
      • initElements

        public static void initElements​(org.openqa.selenium.WebDriver driver,
                                        java.lang.Object page)
        As initElements(WebDriver, Class) but will only replace the fields of an already instantiated Page Object.
        Parameters:
        driver - The driver that will be used to look up the elements
        page - The object with WebElement and List<WebElement> fields that should be proxied.
      • initElements

        public static void initElements​(ElementLocatorFactory factory,
                                        java.lang.Object page)
        Similar to the other "initElements" methods, but takes an ElementLocatorFactory which is used for providing the mechanism for finding elements. If the ElementLocatorFactory returns null then the field won't be decorated.
        Parameters:
        factory - The factory to use
        page - The object to decorate the fields of
      • initElements

        public static void initElements​(FieldDecorator decorator,
                                        java.lang.Object page)
        Similar to the other "initElements" methods, but takes an FieldDecorator which is used for decorating each of the fields.
        Parameters:
        decorator - the decorator to use
        page - The object to decorate the fields of