Hi all, in this article, I will improve my Page Object Model (POM) example by using Page Factory, Page Generator, Chain of Invocations, and JAVA Generics techniques and patterns. These techniques will reduce code repetition and duplicated code blocks. In this way, we can easy to understand, easy to read, and easy to maintain our automation projects.

The PageFactory class helps us to write more clean and maintainable codes. First, we need to find web elements by using below format (@FindBy annotation) in page classes.

You can use, ID, XPATH, CSS, CLASS_NAME and all kind of locator strategies with PageFactory.

Then, we need to initialize our elements when we are instantiating our page class with below code.

In this way,  we don’t need to write “driver.findElement(By.ByCssSelector(“.logo”));” statements. PageFactory class will handle this operations.

Our project is to test login of n11.com website. The details were described in my former POM article.

Ok, now we can start to write our PageGenerator. This is one of our base class and it generates (instantiates) our pages. I instantiate the pages with “PageFactory.initElements(driver, pageClass);” line because I have to initialize (find) all elements when I instantiate (initialize) the page class. Also, I used JAVA generics to get the class of a page and return the generated (instantiated) pages which extend from BasePage class. BasePage class is the base class of all pages.

and we can instantiate pages in test classes as follows.

Above statement instantiates the page and instantiates it’s all elements and then executes its goToN11() method.

Now, Let’s write BasePage Class. In this class, we should write all common features and attributes of all pages. Here, I used method level JAVA generics to writeText, readText, and click methods. In this way, you can either send parameter as By type and Webelement type.

Now, let’s change HomePage class. I used Chain of Invocation technique for goToLoginPage() method. I returned the LoginPage class in this method. In this way, I can use LoginPage Class’s methods after goToLoginPage() method in test classes as shown below.

Also, I used PageFactory’s @FindBy annotation to find signInButton. HomePage.java implementation is as follows.

Now, let’s change LoginPage class. It is shown as follows.

Our page classes implementations finished. Now Let’s write BaseTest and LoginTests classes.

In BaseTest Class, before each test method execution, I did setup operations. They are:

  • Creating a ChromeDriver.
  • Declaring a wait for test methods.
  • Maximize the browser.
  • Instantiate the PageGenerator class.

and at teardown, I quit (kill) the browser.

OPTIONAL: If you are doing single threaded test execution, you can increase your test execution time with below implementation. This time, I only created one driver instance at Class level, not at Method level. This implementation opens only one browser for each test class.

and now, it’s time to write the test method. I used Page Generator, Method Invocation techniques in test methods as I described before. Here is the code:

and that’s all. 🙂

GitHub Link: https://github.com/swtestacademy/PageFactory 

Selenium Webdriver Tutorial Series

Thanks for reading and happy testing!
-Onur