Skip to content

Commit

Permalink
Added json file processor for the test data. (#17)
Browse files Browse the repository at this point in the history
* Added json process for the test data.

* refactor the test data processor

* update the readme doc

* update readme doc

---------

Co-authored-by: Mahmudul Hassan <[email protected]>
  • Loading branch information
maruf571 and Mahmudul Hassan authored Dec 2, 2023
1 parent 405088f commit 1c9e67c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 16 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ The project is structured as follows:
         ├─ config.properties
         └─ testdata
            ├─ login.csv
            ├─ login.json
            └─ products.csv
            └─ products.json
```

## Basic Usage
Expand Down Expand Up @@ -142,7 +144,7 @@ The project is structured as follows:
```
- ### Test Data
The project uses *csv* file to store test data and [*univocity-parsers*](https://github.com/uniVocity/univocity-parsers) to retrieve the data and map it to a Java bean.
The project uses *csv* or *json* file to store test data and [*univocity-parsers*](https://github.com/uniVocity/univocity-parsers) to retrieve the data and map it to a Java bean.
To add configurations for new test data, add a new Java bean in the [*data*](./src/main/java/io/github/tahanima/data) package. For example, let's say I want to add test data for a `User` with the attributes `First Name` and `Last Name`. The code for this is as follows:
Expand Down Expand Up @@ -172,6 +174,18 @@ The project is structured as follows:
Test Case ID,Test Case Description,First Name,Last Name
TC-1,Successful user creation,Tahanima,Chowdhury
```

Alternately you can use a json file `user.json` with the below contents and use it in your tests.
```json
[
{
"testCaseId": "TC-1",
"testCaseDescription": "Successful user creation",
"firstName": "Tahanima",
"lastName": "Chowdhury"
}
]
```
For reference, check [this](./src/main/java/io/github/tahanima/data/LoginData.java), [this](./src/test/resources/testdata/login.csv) and [this](./src/test/java/io/github/tahanima/e2e/LoginE2ETest.java).

- ### Browser
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/tahanima/data/ProductsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public final class ProductsData extends BaseData {

@Parsed(field = "User Name", defaultNullRead = "")
private String userName;
private String username;

@Parsed(field = "Password", defaultNullRead = "")
private String password;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/github/tahanima/e2e/LoginE2ETest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.tahanima.e2e;

import static io.github.tahanima.util.DataProviderUtil.processCsv;
import static io.github.tahanima.util.DataProviderUtil.processTestData;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

Expand All @@ -27,7 +27,7 @@ public final class LoginE2ETest extends BaseE2ETest {
public Object[][] getLoginData(final Method testMethod) {
String testCaseId = testMethod.getAnnotation(Test.class).testName();

return processCsv(LoginData.class, getTestDataFilePath(FILE_PATH), testCaseId);
return processTestData(LoginData.class, getTestDataFilePath(FILE_PATH), testCaseId);
}

@AfterMethod(alwaysRun = true)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/github/tahanima/e2e/ProductsE2ETest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.tahanima.e2e;

import static io.github.tahanima.util.DataProviderUtil.processCsv;
import static io.github.tahanima.util.DataProviderUtil.processTestData;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

Expand All @@ -26,7 +26,7 @@ public final class ProductsE2ETest extends BaseE2ETest {
public Object[][] getProductsData(final Method testMethod) {
String testCaseId = testMethod.getAnnotation(Test.class).testName();

return processCsv(ProductsData.class, getTestDataFilePath(FILE_PATH), testCaseId);
return processTestData(ProductsData.class, getTestDataFilePath(FILE_PATH), testCaseId);
}

@AfterMethod(alwaysRun = true)
Expand All @@ -49,7 +49,7 @@ public void captureScreenshotOnFailure(ITestResult result) {
groups = {"smoke", "regression"},
retryAnalyzer = TestRetry.class)
public void testSuccessfulLogout(final ProductsData data) {
loginPage.loginAs(data.getUserName(), data.getPassword()).clickOnLogout();
loginPage.loginAs(data.getUsername(), data.getPassword()).clickOnLogout();

assertThat(loginPage.getUrl()).isEqualTo(data.getUrl());
}
Expand Down
52 changes: 43 additions & 9 deletions src/test/java/io/github/tahanima/util/DataProviderUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.github.tahanima.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvRoutines;

import io.github.tahanima.data.BaseData;

import java.io.FileInputStream;
Expand All @@ -11,6 +12,7 @@
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
* @author tahanima
Expand All @@ -19,18 +21,17 @@ public final class DataProviderUtil {

private DataProviderUtil() {}

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);
public static Object[][] processTestData(Class<? extends BaseData> clazz, String fileName, String id) {
if (fileName.endsWith(".csv")) {
return processCsv(clazz, fileName, id);
} else if (fileName.endsWith(".json")) {
return processJson(clazz, fileName, id);
}

return dataArray;
return new Object[0][0];
}

public static Object[][] processCsv(Class<? extends BaseData> clazz, String fileName, String id) {
private static Object[][] processCsv(Class<? extends BaseData> clazz, String fileName, String id) {
CsvParserSettings settings = new CsvParserSettings();

settings.getFormat().setLineSeparator("\n");
Expand Down Expand Up @@ -61,4 +62,37 @@ public static Object[][] processCsv(Class<? extends BaseData> clazz, String file

return new Object[0][0];
}

private static <T extends BaseData> Object[][] processJson(Class<T> clazz, String fileName, String id) {
try (Reader 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];
}

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;
}

}
86 changes: 86 additions & 0 deletions src/test/resources/testdata/login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[
{
"testCaseId": "TC-1",
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
"username": "standard_user",
"password": "secret_sauce",
"errorMessage": ""
},
{
"testCaseId": "TC-1",
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
"username": "problem_user",
"password": "secret_sauce",
"errorMessage": ""
},
{
"testCaseId": "TC-1",
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
"username": "performance_glitch_user",
"password": "secret_sauce",
"errorMessage": ""
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Incorrect username and correct password should produce errorMessage",
"username": "username",
"password": "secret_sauce",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
"username": "standard_user",
"password": "password",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
"username": "locked_out_user",
"password": "password",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
"username": "problem_user",
"password": "password",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
"username": "performance_glitch_user",
"password": "password",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Incorrect username and incorrect password should produce errorMessage",
"username": "demo_username",
"password": "demo_password",
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Blank username should produce errorMessage",
"username": "",
"password": "demo_password",
"errorMessage": "Epic sadface: Username is required"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Blank password should produce errorMessage",
"username": "demo_username",
"password": "",
"errorMessage": "Epic sadface: Password is required"
},
{
"testCaseId": "TC-2",
"testCaseDescription": "Should produce errorMessage for locked out user",
"username": "locked_out_user",
"password": "secret_sauce",
"errorMessage": "Epic sadface: Sorry, this user has been locked out."
}
]
9 changes: 9 additions & 0 deletions src/test/resources/testdata/products.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"testCaseId": "TC-1",
"testCaseDescription": "Logging out should redirect to login page",
"username": "standard_user",
"password": "secret_sauce",
"url": "https://www.saucedemo.com/"
}
]

0 comments on commit 1c9e67c

Please sign in to comment.