Elevating Test Automation with JSON Data Providers! ??

Elevating Test Automation with JSON Data Providers! ??


In the realm of test automation, efficiency and simplicity are paramount. Today, let's dive into a powerful technique using JSON files as data providers, making our automated tests more dynamic and maintainable. ????

The Foundation: Lombok and GSON

<!-- Maven Dependencies -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>
        

Utilizing Lombok and GSON streamlines our code, reducing boilerplate and enhancing readability. Now, let's see how we apply this in our test data classes.

The Data Model: BaseData and LoginData

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BaseData {
    private String testCaseID;
    private String testCaseDescription;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class LoginData extends BaseData {
    private String userName;
    private String passWord;
    private String message;
}
        

Efficient JSON Processing with Gson

This class seamlessly reads JSON files, processes the data, and converts it into a format ready for consumption by our test methods. ??? The elegance of Lombok, the efficiency of Gson, and the strategic use of Java - a winning combination for data-driven success! ????

public class DataProviderJSON {

    public static <T extends BaseData> Object[][] processJson(Class<T> clazz, String fileName, String id) {
        try (InputStreamReader reader  = new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8)) {
            ArrayList<ArrayList<? extends BaseData>> testData = new ArrayList<>();
            List<T> jsonData = new Gson().fromJson(reader, TypeToken.getParameterized(List.class, clazz).getType());
            jsonData.forEach(
                    e -> {
                        if (e.getTestCaseID().equals(id)) {
                            testData.add(new ArrayList<>() {
                                        {
                                            add(e);
                                        }
                                    });
                        }
                    });

            return toArray(testData);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Object[0][0];
    }

//Streamlined Data Conversion with toArray() Method	
private static Object[][] toArray(ArrayList<ArrayList<? extends BaseData>> data) {
        int noOfRows = data.size();
        Object[][] dataArray = new Object[noOfRows][1];

        for (int i = 0; i < noOfRows; i++) {
            dataArray[i][0] = data.get(i).get(0);
        }

        return dataArray;
    }


}
        

Data-Driven Testing in Action

public class LogInTest extends BaseTest implements IExecutionListener {

private static final String FILE_PATH = "src//Login.json";

@DataProvider(name = "LogInTestData")
public Object[][] getLogInTestData(final Method testMethod) {
String testCaseId = testMethod.getAnnotation(Test.class).testName();
return processJson(LoginData.class, getTestDataFilePath(FILE_PATH), testCaseId);
    }


 
@Test(testName = "TC-1",priority=1,,dataProvider = "LogInTestData")
public void loginTest(final LogInTestData data) {
  LogInPage login=new LogInPage();
  login.loginTotheApplications(data.getuserName(),data.getpassWoed(), ,data.getmessage());
	    
}  
	   
}        

The magic happens here! We fetch test data from a JSON file based on the test case ID, creating a seamless and dynamic data-driven testing approach.


要查看或添加评论,请登录

Dipankar Dandapat的更多文章

社区洞察

其他会员也浏览了