Make Selenium Easy

How Page Factory In Selenium WebDriver Performs Lazy Initialization

Hello Folks,

We have seen some posts on Page Object Model and Page Factory in Selenium in previous posts. We will see some interesting facts about Page Factory in this post.

Do you know that Page Factor perform lazy initialization? We will see how.

When we use “initElements” method of PageFactory, all page objects of Page Object class are initialized. It does not mean webdriver locates all web elements of page and store it.

Webdriver will locate element only when page object(Web element) is being used or you can say when any action like click etc are performed on web element and it will through noSuchElementException if web element is not found. We will see an example below:

We will pass a wrong locator and will see how PageFactory behaves:

HomePage.Java:

Login.Java:

Console Output:

If you notice console output, you can notice exception is thrown at line number 31 of TC_001_SignIn2.class when user webdriver wants to perform a click on sign in button. It means, webdriver tried to find web element when element is being used not at the time of initialization.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

 

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

6 thoughts on “How Page Factory In Selenium WebDriver Performs Lazy Initialization

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.