Loadable Component Pattern for Better Page Object Model Architecture

Hi all, in this article I will explain you the loadable component pattern in Page Object Model architecture. In our test automation projects generally, we are doing some prerequisite actions to test some scenarios. For example, when we are testing the login, first we need to open the homepage and go to login page, then start to do login actions. Without the loadable component pattern, we do this test as follows:

When we apply the loadable component pattern in our Page Object Model framework, our test class will become like this:

As you see above, now we do not need to specify prerequisite actions explicitly. I mean, we do not need to write HomePage class’s methods which opens the login page:

So, how can we use Loadable Component Pattern? Let’s start to learn.

First, we need to extend our page classes with LoadableComponent abstract class.

LoadableComponent class’s structure is shown below:

It is an abstract class with load(), isLoaded(), and get() methods. When we extend this class, we have to implement load(), and isLoaded() methods and we can use get() method to obtain our class when it is loaded based on our implementations.

After extending LoadableComponent class, now we need to implement load() and isLoaded() methods in HomePage and LoginPage classes. In load () method, we need to declare the actions which load the page and in isLoaded() method, we need to check that the page has been loaded or not. Before this, let’s look at the project structure:

BasePage is our common Page class which comprises of the common functions of all pages.
HomePage is home page class.
LoginPage is login page class.
TestListener is our test listener Class for taking action when test and suite started, finished, failed, skipped, passed.
BaseTest is the common test class.
LoginTest is login test class.
ExtentManager is for ExtentReports.

Let’s look at some classes’ details. First, let’s start with BasePage. We declared common methods such as click, sendText, getText, etc.

HomePage Class:

Here, I implemented load() and isLoaded() methods which belong to LoadableComponent abstract class. I also instantiate BasePage class’s object in the constructor to use its methods via basePage object.

LoginPage Class:

Here, I implemented load() and isLoaded() methods which belong to LoadableComponent abstract class. I also instantiate BasePage class’s object in the constructor to use its methods via basePage object. I also use HomePage class’s goToLoginPage() method at load() block. In order to reach that method, I need to get that class as parent. I assigned the parent object in the constructor and use it at load() method.

parent.get() -> gets the HomePage class

and then, I used its goToLoginPage() method as -> parent.get().goToLoginPage()

In this way, I can go to LoginPage() class.

After, going to LoginPage, we need to check that the page is loaded? We can check this at isLoaded() method.

assertTrue(“LoginPage is not loaded!”, driver.getCurrentUrl().contains(loginURL));

Now, let’s go on BaseTest class. In this class, we can declare the before and after operations at class level.

BaseTest Class:

and now, it is time to write a test method.

LoginTest Class:

Here, I did comon prerequisites for each test in @BeforeMethod block. I can reach the loginPage with below line:

loginPage.get();

This method, firsts call’s LoginPage class’s load() method. In this method, parent.get() part calls HomePage class’s load() method and gets the HomePage class then calls its goToLoginPage() method to load LoginPage().

@Override
protected void load() {
parent.get().goToLoginPage();
}

In @Test blocks we can only call LoginTest page’s methods to fulfill login operation and test scenarios. In this way, we don’t need to explicity call HomePage class’s methods to go/open/load LoginPage.

I do not want to share here ExtentManager and TestListener classes. They are not related to the Loadable Component pattern. But, you can find the full implemented project here: https://github.com/swtestacademy/loadablecomponentexample

You can also learn the SlowLoadableComponent Pattern via this article: http://www.swtestacademy.com/slowloadablecomponent-pattern/

Thanks.
Onur Baskirt

By |2018-12-04T22:35:48+00:00June 7th, 2018|Selenium Tutorials|1 Comment

About the Author:

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as a research assistant, design verification engineer, and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. After Teknosa, he worked as Head of Software Testing and Manager of two Development Teams at Kariyer.net. Now, he is working as a Senior Technical Consultant at Emirates Airlines in Dubai. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

One Comment

  1. Travis December 20, 2018 at 10:01 am - Reply

    In my opinion, using Generic here is too much. Maybe we need two abstract method load() and isLoaded() for page model classes then we implement these two method accordingly to their intention.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.