We have seen below topics as of now:
In this post we will see inbuilt page object model in selenium webdriver.
Selenium webdriver has inbuilt Page Object Model called Page Factory. It provides @FindBy annotation to create object repository.
What is @FindBy annotation?
- Annotations in java are used to provide additional information.
- @FindBy annotation is used to locate web element using any locators available in selenium webdriver (id, xpath, css etc).
- It is used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements.
- It is used in conjunction with PageFactory this allows users to quickly and easily create PageObjects.
Syntax: You can use @FindBy annotation in two ways:
- Direct way:
@FindBy(LocatorStrategy=”Locatorvalue”) WebElement elementName;
Where LocatorStrategy may be any one from Id, name, className, tagName, css, xpath, linkText, partialLinkText.
2. Using “how” and “using”:
@FindBy(how = How.<LocatorStrategy>, using = “<Locatorvalue>”) WebElement elementName;
You can use any syntax as per your convenience.
Same way we can use @FindBy annotation for same list of web elements as shown below:
@FindBy(tagName = “a”) List<WebElement> links;
@FindBy(how = How.TAG_NAME, using = “a”) List<WebElement> links;
How to initialize page objects(web elements) defined using @FindBy annotation?
- We initialize page objects using PageFactory class. It is factory class to make using Page Objects simpler and easier.
- It has overloaded static methods called “initElements” which is used to initialize page objects.
- Overloaded methods are as below:
- We will see behavior of below methods:
- public static <T> T initElements(WebDriver driver, java.lang.Class<T> pageClassToProxy)
2. public static void initElements(WebDriver driver, java.lang.Object page)
- It instantiates an instance of the given class/page object, and set a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared.
- By default, the element or the list is looked up each and every time a method is called upon it. This is the reason it is called lazy initialization. It looks for the element when a method is called on it. It also helps in handling staleElementReferenceException as it looks up for web element each and every time a method is called upon it. So, if page gets refreshed or reloaded.
- We can change the above behavior by mentioning another annotation called CacheLookup as shown below:
4. After mentioning CacheLookup , initElements will cache web element once it is located and will not search again and again when required.
5. Suppose you have a page class named HomePage.class, you can use any above method in below ways:
If you try to use first and second way in a constructor you will get exceptions java.lang.reflect.InvocationTargetException and java.lang.StackOverflowError.
6. An exception will be thrown if the class cannot be instantiated.
We will convert example of last post in PageFactory as shown below:
That’s it guys. Hope, concepts will be clear. It is internal logic of working of PageFactory.
We will see some interesting things about PageFactory in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.