Common Design Patterns every Test Automation Engineer should know

Common Design Patterns every Test Automation Engineer should know

In software design, design patterns are created to solve common problems. This doesn’t seem to be widely discussed in software automation, because the topic sounds complicated. There are sophisticated design patterns used to solve complex issues in software development. Also there are easy to understand and easily adoptable design patterns that can significantly improve readability and maintainability of our test automation code. In this article, you will see complete details of Page Object Model (POM) design pattern with Page Factory in selenium with practical examples.

Page Object Model

POM is a design pattern which has become popular in Selenium Test Automation. It is a widely used design pattern in Selenium for enhancing test maintenance and reducing code duplication. To get started with POM, I would like to take you through the following basic example.


What’s your first name?
Login

Last name?

Page Object

Tell us more about your family

Everyone from my family is a page Object.

Each of us knows everything about a page.

The page that I specialize in is the login page.

My brother, Result Page Object Knows about the results page.

And our sister, Detail Page Object is the expert on details page.


What makes you an expert in login pages?

I know a lot about login page, I know the info that makes the page unique, the title and url.

I also know about elements of the login page, their values and attributes. And my knowledge does not end at login page information.

I can tell this information to anyone interested in it. Just the other day, someone from the unit tests family asked about these details.


Awesome. What else do you know about the login page?

I know what can be done on a page.

Like clicking links, typing in a textbox, selecting values in a listbox. And to see what an expert I am, I can even do these things on behalf of others.

The same unit test that asked the other day about the page title and url wanted more things from me.


Such as…

First let me tell you his name. It is testThatLoginWorks. Weird name, isn’t it?

So, he wanted me to type some text in the username textbox.

Then, to type other text in the Password textbox. And finally, to click the login button. I did so many things for him.
That was a busy day for me.


So you know many things about the login page. Do you know about other pages as well?

Login page is the only thing that I know of.

About other pages, you should talk to the other members of the Page Object family.

My brother, my sister and the everybody else.

Each of them is an expert in one page as well.



Excellent. Can you summarize for our audience what you do best?

I am the number 1 expert in login page.

I know all the information about the login page and its elements. I also know everything that can happen on a login page.

And I can even do these things when asked nicely. I am a Page Object.

Thank you very much!

Consider the below example of simple selenium script which has applied POM concept.

public class GmailLoginPage {
 
WebDriver driver;

By userName = By.name("uid");

By password = By.name("password");

By login = By.name("btnLogin");

public GmailLoginPage (WebDriver driver){

   this.driver = driver;

}

//Set user name in textbox

public void setUserName(String strUserName){

   driver.findElement(userName).sendKeys(strUserName);

}

//Set password in password textbox

public void setPassword(String strPassword){

	driver.findElement(password).sendKeys(strPassword);

}

//Click on login button

public void clickLogin(){

  	driver.findElement(login).click();
}

/**

 * This POM method will be exposed in test case to login in the application

 * @param strUserName

 * @param strPasword

 */

public void loginToGmail(String strUserName, String strPasword){

   //Fill user name

   this.setUserName(strUserName);

   //Fill password

   this.setPassword(strPasword);

   //Click Login button

   this.clickLogin();	

}

Why POM should be used?

Here are the main advantages of Page Object Pattern using:

  • Easy to Maintain
  • Easy Readability of scripts
  • Reduce or Eliminate duplicate
  • Reusability of code
  • Reliability

Page Factory Model

Page Factory is an inbuilt Page Object Model concept for selenium webdriver but it is optimized. Using Page Factory under test file, you can access these page objects in other java file. Used annotations @FindBy to find WebElement and initElements method to initialize web elements. Consider the below example of simple selenium script which has applied Page Factory concept.


    public class GmailLoginPage {

   /**

	* All WebElements are identified by @FindBy annotation

*/

   WebDriver driver;

   @FindBy(name="uid")

   WebElement userName;

   @FindBy(name="password")

   WebElement password;

   @FindBy(name="btnLogin")

   WebElement login;

  
   public GmailLoginPage(WebDriver driver){

  	this.driver = driver;

  	//This initElements method will create  all WebElements

  	PageFactory.initElements(driver, this);

   }

   //Set user name in textbox

   public void setUserName(String strUserName){

  	userName.sendKeys(strUserName);  	

   }

  
   //Set password in password textbox

   public void setPassword(String strPassword){

   password.sendKeys(strPassword);

   }

  
   //Click on login button

   public void clickLogin(){

     	login.click();

   }


   /**

	* This POM method will be exposed in test case to login in the application

	* @param strUserName

	* @param strPasword

*/

   public void loginToGmail(String strUserName, String strPasword){

  	//Fill user name

  	this.setUserName(strUserName);

  	//Fill password

  	this.setPassword(strPasword);

  	//Click Login button

  	this.clickLogin();
        
   }

}

References

What is the page object in Selenium?

Design Patterns In Test Automation World

Page Object Model (POM) & Page Factory: Selenium WebDriver Tutorial

Dinuka Abeysinghe

Senior QA Engineer


Administrator

Related Blog post

The Future of Software QA

The Future of Software QA

Thaveesha

August 24, 2020

Leave a Reply

Your email address will not be published. Required fields are marked *

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