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:
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.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning