An image

Corporate Blogs

Implementing Page Object Model(POM) With Ruby Cucumber & Watir

E-Commerce Businesses Need a Mobile App


What Is Page Object Model (POM)

Page Object Model (POM) is a design pattern, nowadays the POM becomes more popular in automation testing area. As it helps to create Object repository for web User Interface(UI) elements and it eliminates the code duplication to increase the maintainability of the code. Parallely, it also acts as an interface to a page of our Application under test.

We can create separate Page Object Class for each page of our application, that contains the methods that represent the services available on the page and this will also help us to find the HTML elements of that page. Usually, the test code will never access the browser instance and HTML elements directly whereas it will only access through the Page object class.

The main advantage of POM is, that we can change or rewrite any web page on which we no need to change any test. So, we need to change the code only within the page object class which needs to be changed. Alongside, POM supports both Selenium and Watir Webdriver.

Why We Should Use POM 

Handling one test file is easy to maintain! But how about maintaining 100+ test files at same time? Can you imagine handling it? No right?! Of course, it sounds bad! That’s why we’re going to use POM here.
For example, Let’s consider that one of our application contains 10+ pages.  Let’s assume that each page has to display its username after the login.  In this scenario, we must test whether the username is present or not. One of the simple yet the toughest problem with test script maintenance is that we will use the same page element in all our 10+ test scripts. So, If any changes occurs in that element then the test will be completely failed in all the 10+ test scripts. As a result, we should update the change in all the 10+ test scripts. Now you guys come to know how time consumable it is. On the other hand, it’s not easy to maintain also. For avoiding this we can use Page Object Model (POM).

In this tutorial, we are going to integrate POM with Watir Webdriver and cucumber.

In case, If you are new to cucumber and don’t have any idea of where to start then you can follow this guide, where i have explained completely about the Cucumber & it’s web automation testing.

A Guide for Web Automation Testing With Ruby Cucumber And Watir


Implementing Page Object Model (POM) With Ruby Cucumber

To implement POM, we need to create a project cucumber-test first and you can use the following code to create it,


Then create a file named with ‘Gemfile’ and then add the following lines into Gemfile.


Run the below code to install gems for our testing.


Now we can initialize the cucumber for our project using cucumber –init comment, as it will help us to create the cucumber project structure,


Add the following lines in features/support/env.rb file

Now we’re ready to implement the Page Object model for Amazon search functionality with the below code.

Note: To give you the simplified solution here i have implemented POM on my previous code of cucumber blog. Similarly, you can implement it with any of your cucumber projects.

Create a file amazonsearch.feature under features/ directory and add the following lines.




Now you can create ‘pages’ directory under ’features’ directory. Normally page object file has two parts, one is Declaration part and another one is the implementation part. Therefore, if we try to integrate it with cucumber then the declaration part of the page class will be page object file and implementation part will be step definition file.

Now create search_page.rb file under features/pages directory, then add the following code

Create a file named “amazon_search_step.rb” under features/step_definitions directory and add the following lines.


As you can see in the step definition file, that we accessed all the elements from the page object class. So, if any changes occurs in UI then we don’t need to change the step definition file. Instead, we need to change the code only within the Page Object class file.

Now you can run the cucumber test using the following command.


Page object model with Ruby Cucumber Watir

Advantages of Using Page Object Model (POM)

Reduces Code Duplication -> As it has non-brittle test code which will not allow us to give duplicate name so it helps us to avoid code duplications.

Provides More Readability -> We can define the page using Page Object, also these methods provide access to the page dynamically, which will work on all pages.

Improves Maintainability -> If you want to change the locator name, you can change it in the definition part of the page. And this will reflect in all the tests so we no need to change the step definitions and feature files too.

Supports Code Reusability -> Basically, Page object class will not depend on test case. We can use the same Page object repository to integrate with different testing frameworks like JUnit, Cucumber, Capybara, etc.

Disadvantages Of Using POM

Takes time -> It takes time to define all locators in a class
High setup -> It requires more manual efforts to set up.

Like What You're Reading ?

Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
Senior Software Engineer | Around 4 Years of experience in Web development and Testing arena. Plus, hands on expertise in Ruby, Ruby on Rails, Watir, Ruby Cucumber.

Leave a Comment

You have an Idea. We have the Solution.

We help business evolve with lates technologies and infrastructures tailored to their needs and market trends.