BACK

Three Gold Bracelets In Test Automation: POM, Selenium, Cucumber

FEBRUARY 21, 2023

There are lots of ways to design and write test automation but nowadays people prefer to use POM (“page object model”), seleniumand cucumber together. Of course it does not mean we can use them everywhere and every time but we can prefer to use them together especially for web application.  If I need to give a short definition, POM is a design pattern, which is popular in test automation. It helps us reducing code duplication and also enhancing test maintenance. Selenium is the most popular open source library/tool among web applications. Finally, cucumber is a tool, which supports BDD(“Behavioural Driven Development”).

In this article, I will give a simple example for how to write automation code using three of them. First of all, I prefer using Intellij IDEA and Maven project and Java for my automation test, but you can use whatever you want (Eclipse also preferable).  Before writing the automation code, we need testing the cases. At this stage cucumber helps us. In our project, we need to add some library (Junit, selenium-java, cucumber-junit, cucumber-java, cucumber-core, cucumber-jvm-deps, gherkin). Please do not forget to include them. Cucumber and BDD help to solve communication problems between two different departments, and also help us to write test cases.

The test Case details: One person wants to search for the word “selenium” on google. Therefore, we need to add a directory which is named “features” and create “Search.feature” file under this folder. At this stage, we can start to write our case inside the feature file. We use gherkin language (which is a Business Readable, Domain Specific language) for common language.

You can see that, when we write cucumber test case, we just need “Scenario”, “Given”, “When”, “Than” tag. Inside tags, we can easily write our scenario. After all, scenario helps us to run our code block.

Not specific to test automation, but every code must be readable for everyone. It means that, we need to design our code logically and as simple as possible. Adding comments to code is a good way giving more information on the details.

As seen below, we keep the variables in one page (one or more) in order to easily modify the variables. So before writing our test, we need to add TestObject class (you can prefer another name).

The example above shows us how to define variables, which are most commonly used. After that, we need “Pages” package to define our pages. Pages package is mostly used for web applications that contain menu and more pages. In this example, we will keep the steps simpler and do a google search without any need to use too many menus and pages. When doing a search on google, there some steps that we must follow. Firstly, we open a browser, then write text in text area and finally click the button for search. As seen above I wrote a selenium code for each step of google search.

The key point is that two classes needed for POM.  The structure will be seen as below.

After writing the page class, we need to add SearchTestRunner and StepImplementation classes for using test steps and run them.

            In the first class (“cucumberTestRunner”), I clarified my cucumber class and steps and feature class for running our test easily. In addition to this, the second class shows us the test steps and what the steps are doing. We can see the methods calling from pages in the second class.  This simple example shows us how to use POM, selenium and cucumber together.

This type of writing gives us the test pattern that is well-known and popular in the world. Although it is up to you to choose any methods, it is always advantageous to follow the methods that are common today and being aware of the new trends.