Test Automation - Improve Code Readability Using Enumeration

Test Automation - Improve Code Readability Using Enumeration

Introduction

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

Programing languagesJava

Automation FrameworkSelenium

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.

Happy testing!

very good way not to use hardcoded data

Like
Reply

I cannot agree more! I am also using enums heavily in our automation. In addition to what you have mentioned, I would add that in case of API testing values can be decorated by annotation @SerializedName, for example: -- Directions.java -- import com.google.gson.annotations.SerializedName; public enum Directions {   @SerializedName("north") NORTH,   @SerializedName("south") SOUTH,   @SerializedName("east") EAST,   @SerializedName("west") WEST; } -------------------- And in this case, it will be more convenient to use enum in POJOs during serialization/deserialization of request/response objects. At the same time, I found it useful to add a static method to request enum value by its 'friendly name'. It will simplify your cucumber/bdd step definitions - you just need to add definition into your configuration: -- Directions.java -- import com.google.gson.annotations.SerializedName; import java.util.Arrays; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor public enum Directions {   @SerializedName("north") NORTH("north"),   @SerializedName("south") SOUTH("south"),   @SerializedName("east") EAST("east"),   @SerializedName("west") WEST("west");   private String friendlyName;   public static Directions getDirectionByItsFriendlyName(final String friendlyName) {     return Arrays.stream(values())         .filter(d -> d.getFriendlyName().equalsIgnoreCase(friendlyName))         .findFirst()         .orElseThrow(() -> new RuntimeException(                  String.format("No directions found for value %s", friendlyName)));   } } -------------------- -- Configurer.java -- public class Configurer implements TypeRegistryConfigurer {  ...   typeRegistry.defineParameterType(new ParameterType<>(     "direction",     "north|south|west|east",     Directions.class,     Directions::getDirectionByItsFriendlyName)   );   ... } -------------------- So, in your step definition, you can use placeholder {direction} that will be automatically converted into a proper enum value: -- DirectionSteps.java -- ... @And("It is located toward the {direction}") public void itIslocatedToward(Direction direction) {   ... } ... -------------------------

To view or add a comment, sign in

More articles by Nir Tal

Others also viewed

Explore content categories