Saturday, February 25, 2023

Abstracting the Test Layer using TestNG

The library architecture is popular among test automation framework designers. To follow this pattern, engineers decompose and abstract the automation logic at various conceptual levels, such as at the page, the component or at the element level. But usually, the test layer is left out of these decomposition efforts.

Leaving abstraction out of the test layer can in time lead to duplication of the logic as well as having to spend more time than what is required to script new scenarios. This post will discuss how the test layer can be abstracted and decomposed using TestNG.


High-level Steps

  1. Start with designing and implementing test cases so that they are atomic
  2. Encapsulate the test layer by scripting the test steps and adding assertions required to validate the component. Then put a mechanism in place to read the test data from an object that can be passed from one test class to another as required.
  3. Finally, use the Factory feature in TestNG to compose the larger scripts using the test classes.
Let’s take a crude example of validating a web page with a dropdown[1]. For this example, I have identified the two test cases seen below,
  1. Validate that the dropdown page loads the dropdown and that the values are as expected.
  2. Validate that the dropdown allows the user to select the option he wants to select.
The test layers for the two test cases were scripted as seen below and were joined together using the factory annotation in the DropDrownTest class. Notice how the same test data object is passed between the two test cases.


Test Class 1


Test Class 2


Factory Class


[1] - https://the-internet.herokuapp.com/dropdown

[repo] - https://github.com/handakumbura/SeleniumAutomationEmployerProfile/tree/feature/atomic_test_cases




What's in my Bag? EDC of a Tester