Hi all, in this tutorial, I will explain to you how to use PageGenerator and LoadableComponent to create a better Page Object Model based Selenium Webdriver test automation projects. By using LoadableComponent pattern, we can easily get the pages without pre-initializations. I described this technique in this article in detail. When we use LoadableComponent pattern with PageGenerator we will easily use the page functions without explicit instantiations.

I will explain this technique on n11.com website. Our test scenario is simple, opening the home page and try to login the site with invalid inputs and check the warning messages. Our main pattern is POM (Page Object Model) and we will construct our project based on POM structure.

First, I want to explain to you PageGenerator class. I called it as “Page” class. In this class, we can create (instantiate) pages. If a page already instantiated with a specific Webdriver instance, we will use the pre-instantiated one instead of the creating a new page instance. So, how to do this?

I will use a ConcurrentHashMap to hold Webdriver and {PageName, PageInstance} pair as shown below. pageHashMap = {Webdriver, {PageName, PageInstance}}

I will have four methods to manage instantiate page instance and save the instantiated page instance with its driver. These are:

setPageHashMap

If a current Webdriver instance is not in the pageHashMap, then this method create a new {Webdriver, {PageName, PageInstance}} pair and put it into the pageHashMap. If current Webdriver exists in the pageHashMap and if the current driver did not instantiate given page before, then put the new PageName and PageInstance into the pageHashMap.

getPageHashMap

It returns the page instance by given driver and page name. If the current driver instance does not exist then it returns null.

createPage

This method gets page name and page class and gets the page instance by using getPageHashMap method. If the page instance is not null, it returns the page instance. If the page instance is null, it first instantiates the page by using current webdriver instance and page class, then it puts the {current driver, {PageName, PageInstance}} pair to the pageHashMap and finally, it returns the Page object by using getPageHashMap method.

instantiateNewPage

I got this method from PageFactory class and modified it to generate new page instance.

Finally, we can generate page class’s in this way:

and when we combine them our page generator class (Page Class) will look like this:

Page Class (Page Generator)

BasePage Class

Let’s go on with BasePage class. We will declare the main page operations in this class such as click, write text, and read text, etc.

HomePage Class

In HomePage class we extend the LoadableComponent class and instantiate the BasePage class in the constructor. In this way, we can use BasePage class’s methods. Also, we implement load() and isLoaded() method’s of the LoadableComponent class. Finally, we define page related functions.

LoginPage Class

In HomePage class we extend the LoadableComponent class and instantiate the BasePage class in the constructor. In this way, we can use BasePage class’s methods. Also, we implement load() and isLoaded() method’s of the LoadableComponent class. Finally, we define page related functions.

Now, lets go on with test classes.

BaseTest Class

It is the base class of all tests. We define global test setups in this class. For thread-safe parallel test execution, we use ThreadLocal concurrent map. Also, we instantiate page class to generate page classes.

LoginTests Class

In LoginTests class, we do not need to instantiate the pages as you see in the code and also we can get the login page by using LoadableComponent class’s get() method. When we need to reach the page methods, we use the page class to reach them easily.

ExtentManager is used for ExtentReports.

OptionsManager is used for DesiredCapabilities settings.

ThreadLocalDriver is used for thread-safe parallel execution.

You can reach all codes in projects GitHub page. I don’t want to explain that parts in this article. I explained these classes in other articles.

Project GitHub Page: https://github.com/swtestacademy/pagegenerator-loadablecomponent-pom

Thanks.
Onur Baskirt