test automation using enumeration

Introduction

In this article, I will review the implementation of enumeration that will help us improve our code readability.

Programing languages: Java

Automation Framework: Selenium

What is Enumeration?

From Oracle documentation :

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type’s fields are in uppercase letters.

In the Java programming language, you define an enum type by using the enum keyword

Implementation

First will take a look at a simple Selenium code that navigates to a menu option.

In the page object class, we identify a list of menu items and create a method that navigates to an option using a value that represents the option index.

No alt text provided for this image

In the test class the usage of the navigation method is:

No alt text provided for this image

We need to refactor the navigation method in order to improve the readability, we need to create an enum with values that represents the menu options:

No alt text provided for this image

Then we can refactor our navigation method in the page object class:

No alt text provided for this image

In the test class the usage of the refactored navigation method is:

No alt text provided for this image

Our test code now looks more readable using enumeration to define our menu navigation options instead of using the list indexes.

In conclusion

In this article, we reviewed the usage of enumeration, we refactored a navigation method and saw the improvement of our code readability.

Leave a Reply

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