A Complete Keyword-Driven Test Automation Framework with JSON Format.
Test Automation, Keyword driven
Join the DZone community and get the full member experience.
Join For FreeExcel is used for Data-driven and Keyword-driven Testing in most of my test automation projects before since it is good to manage data and to use but it comes with its own limitations such as:
- MS Office needs to be installed on the system where the tests are being executed.
- It’s about performance issue when loading the large excel file.
From the above issue, I came to an idea to build Keyword-driven testing using another kind of data provider - JSON.
During the development phase, I found it’s not only about the data provider, but also it’s
A Complete Keyword-Driven Automation Testing Framework with JSON Data Provider.
A framework helps the automation engineer build the complete test automation easily by integrating the various automation libraries, tools and supporting the way to execute Test Suite/ Test case using JSON for Keyword-Driven. There are 2 crucial characteristics of this framework.
Maintainability
- Separates test automation implementation from test case design.
- Make clear the test cases from test steps in the readable format (JSON).
- Reusability code by the defined keywords and Page Object Pattern.
- Generate beautiful, interactive and detailed HTML reports.
Extendability
- Integrability of the generating the test suites/ test cases/ test step content from Test Management tools (Zephyr for Jira,…) to JSON file following the defined format is easier than code scripts. Addition, it’s more visual and readable.
- Easy to import the test result to the test management systems.
- Easy to integrate the test execution to CI systems.
The automation libraries, tools are used in this framework.
- TestNG — A Testing framework
- Selenium WebDriver — A library you can use to test Web Application.
- Rest Assured — A library you can use to test HTTP-based REST services.
- Extent Report 4.0 — A framework for creating a test report
Execution Diagram
Below diagram illustrates how the project works when running an execution file.
The TestNG runs the Test Suite file which is formed by XML file.
Using JSON for Data Driven
A test suite is defined in a JSON file that contains a collection of test cases, and a collection of test step with specified parameters in respectively.
Test suite/ test case format.
{
"suiteName": "Verify [Add To Cart] feature",
"suiteDescription": "",
"testCases": [
{
"testId": "IN_AC1",
"testName": "Verify shopping cart counter (select 1 item)",
"testDescription": "",
"testObjectives": "Verify 1 item is added to cart",
"note": "",
"testSteps": [
{
"name": "Login with standard_user",
"class": "LoginAction",
"method": "login",
"parameters": {
"user": "standard_user",
"password": "secret_sauce"
}
},
{
"name": "Add item [Sauce Labs Backpack] to cart",
"class": "InventoryAction",
"method": "addToCartTest",
"parameters": {
"Item Names": ["Sauce Labs Backpack"],
"Counter": 1
}
}
]
}
]
}
How to use?
- Define Action Class and Action method in keywords folder. Ex: Create Class InventoryAction Action contain addToCartTest method.
- Specify the JSON file path in Data Provider function. And then, in test method, we can get param value from the specified test step.
Apply Page Object Pattern
Based on the series of Test Automation Design Pattern from site “https://www.automatetheplanet.com/advanced-page-object-pattern/", this framework supports 3 base classes are BaseUI<M,V>, BaseUIMap and BaseUIValidator. So a UI Control as page/modal dialog or any UI control should be a set of 3 classes that extent to above 3 classes.
- A class extents BaseUI<M,V>: where defining the highlevel action methods in page/modal dialog/ control.
- A class extents BaseUIMap: where finding the elements in page, control or modal dialog for mapping, and this class is used by Map() functions in BaseUI class and BaseUIValidator class.
- A class extents BaseUIValidator: where defining the validate methods for the class.
Implement test methods for each test suite/ test cases
BaseTest class is supported for something such as:
- Initialize Test Report in @BeforeSuite method.
- Create and add test suites/ test cases information in @BeforeMethod method.
- Log test status for each test step, test case (Pass, Fail, Skip).
- Support option of starting browser at @BeforeMethod, and options of stop browser at @AfterMethod or @AfterClass
- Loading the specified config file in @BeforeSuite.
So each Test Class should extents to BaseTest class.
Test Report
- The Test Report should be formed like the below image, with Test Case name is retrieved from JSON file.
Some words
- Here is the source code of this framework: https://github.com/sugia279/AutomationTestingFramework
- Thanks for all of the support you guys (my colleagues) given me to complete this framework regardless of how big or small. Especially you Nguyen Trong Tuyen who has contributed many good ideas for this.
- If you interest in this article, can you give me a clap and leave your comment or contact me at hiensu304@gmail.com, and then we can discuss more.
- Next story, I’d like to show you how do we use this framework for rest API testing.
Opinions expressed by DZone contributors are their own.
Comments