Test Automation - Reducing The Number Of Test Classes Using The Adapter Pattern
Introduction
In this article, I will review the usage of a design pattern called the Adapter pattern, according to baeldung:
An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s interface.
The main motive behind using this pattern is to convert an existing interface into another interface that the client expects. It’s usually implemented once the application is designed.
The Problem
In my automation testing, I needed to write some test cases that perform different form submissions from different pages, each form also required sending its unique data. The automation then performs the form submission and validates that a success message appears.
At first, I created different test classes for each form, the implementation was:
First Form test class:
Second Form test class:
At this implementation - every new form developed will result in the increment of the number of test classes. these tests perform almost exactly the same thing.
I needed a generic solution that will help me to reduce the number of test classes for these forms.
The Solution - Using the Adapter pattern
Creating the Interface
I created a form interface to function as a generic adapter of the form creation.
The interface configure method receives the following parameters:
- The wait instance that is currently in use.
- List of the form input fields which are needed to be mapped when we are sending the relevant form data.
The implementation is:
Each form must implement the form interface and configure the form. I am using Lombok and the builder pattern to build the form object you can further read on this here, the first form class now looks like this:
the second form class now looks like this:
Test Implementation using TestNG DataProvider - Data-Driven testing
In the tests class, we are using TestNG and DataProvider which is a method TestNG has provided to perform data-driven testing. the DataProvider method that I created in the test class provides the tests the following data:
- Which form to navigate.
- Form object that is required for the form configuration.
The implementation of the test class is:
With this solution - All the form submission tests will be performed in the same test class, for each new form the only change needed is to add a new data object in the dataProvider method.
Further reading on TestNG dataProvider can be found in TestNG documentation.
In conclusion
In this article, we reviewed the adapter design pattern and its advantages - the reduction of test classes and the ability to create a generic interface that is improving the code readability.
Further reading on Java design patterns can be found in this link.
Happy testing!
קורס אוטומציה למתקדמים "טייס אוטומטי"- ההרשמה נפתחה לנובמבר, הקורס זמין גם בצורה מלאה הכוללת תמיכה בגרסה דיגיטלית.
לחץ פה על מנת לראות את תכני קורס אוטומציה למתקדמים.
Test Automation Expert
5ygreat example!
סמנכ"ל שיווק ודיגיטל (CMO / CDO)
5yיפה מאוד