Skip to content

Commit 1c9e67c

Browse files
maruf571Mahmudul Hassan
and
Mahmudul Hassan
authored
Added json file processor for the test data. (#17)
* 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]>
1 parent 405088f commit 1c9e67c

File tree

7 files changed

+159
-16
lines changed

7 files changed

+159
-16
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ The project is structured as follows:
9999
         ├─ config.properties
100100
         └─ testdata
101101
            ├─ login.csv
102+
            ├─ login.json
102103
            └─ products.csv
104+
            └─ products.json
103105
```
104106

105107
## Basic Usage
@@ -142,7 +144,7 @@ The project is structured as follows:
142144
```
143145
144146
- ### Test Data
145-
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.
147+
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.
146148
147149
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:
148150
@@ -172,6 +174,18 @@ The project is structured as follows:
172174
Test Case ID,Test Case Description,First Name,Last Name
173175
TC-1,Successful user creation,Tahanima,Chowdhury
174176
```
177+
178+
Alternately you can use a json file `user.json` with the below contents and use it in your tests.
179+
```json
180+
[
181+
{
182+
"testCaseId": "TC-1",
183+
"testCaseDescription": "Successful user creation",
184+
"firstName": "Tahanima",
185+
"lastName": "Chowdhury"
186+
}
187+
]
188+
```
175189
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).
176190

177191
- ### Browser

src/main/java/io/github/tahanima/data/ProductsData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public final class ProductsData extends BaseData {
1414

1515
@Parsed(field = "User Name", defaultNullRead = "")
16-
private String userName;
16+
private String username;
1717

1818
@Parsed(field = "Password", defaultNullRead = "")
1919
private String password;

src/test/java/io/github/tahanima/e2e/LoginE2ETest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.tahanima.e2e;
22

3-
import static io.github.tahanima.util.DataProviderUtil.processCsv;
3+
import static io.github.tahanima.util.DataProviderUtil.processTestData;
44

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

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

30-
return processCsv(LoginData.class, getTestDataFilePath(FILE_PATH), testCaseId);
30+
return processTestData(LoginData.class, getTestDataFilePath(FILE_PATH), testCaseId);
3131
}
3232

3333
@AfterMethod(alwaysRun = true)

src/test/java/io/github/tahanima/e2e/ProductsE2ETest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.tahanima.e2e;
22

3-
import static io.github.tahanima.util.DataProviderUtil.processCsv;
3+
import static io.github.tahanima.util.DataProviderUtil.processTestData;
44

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

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

29-
return processCsv(ProductsData.class, getTestDataFilePath(FILE_PATH), testCaseId);
29+
return processTestData(ProductsData.class, getTestDataFilePath(FILE_PATH), testCaseId);
3030
}
3131

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

5454
assertThat(loginPage.getUrl()).isEqualTo(data.getUrl());
5555
}

src/test/java/io/github/tahanima/util/DataProviderUtil.java

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.github.tahanima.util;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
35
import com.univocity.parsers.csv.CsvParserSettings;
46
import com.univocity.parsers.csv.CsvRoutines;
5-
67
import io.github.tahanima.data.BaseData;
78

89
import java.io.FileInputStream;
@@ -11,6 +12,7 @@
1112
import java.io.Reader;
1213
import java.nio.charset.StandardCharsets;
1314
import java.util.ArrayList;
15+
import java.util.List;
1416

1517
/**
1618
* @author tahanima
@@ -19,18 +21,17 @@ public final class DataProviderUtil {
1921

2022
private DataProviderUtil() {}
2123

22-
private static Object[][] toArray(ArrayList<ArrayList<? extends BaseData>> data) {
23-
int noOfRows = data.size();
24-
Object[][] dataArray = new Object[noOfRows][1];
25-
26-
for (int i = 0; i < noOfRows; i++) {
27-
dataArray[i][0] = data.get(i).get(0);
24+
public static Object[][] processTestData(Class<? extends BaseData> clazz, String fileName, String id) {
25+
if (fileName.endsWith(".csv")) {
26+
return processCsv(clazz, fileName, id);
27+
} else if (fileName.endsWith(".json")) {
28+
return processJson(clazz, fileName, id);
2829
}
2930

30-
return dataArray;
31+
return new Object[0][0];
3132
}
3233

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

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

6263
return new Object[0][0];
6364
}
65+
66+
private static <T extends BaseData> Object[][] processJson(Class<T> clazz, String fileName, String id) {
67+
try (Reader reader = new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8)) {
68+
ArrayList<ArrayList<? extends BaseData>> testData = new ArrayList<>();
69+
List<T> jsonData = new Gson().fromJson(reader, TypeToken.getParameterized(List.class, clazz).getType());
70+
jsonData
71+
.forEach(
72+
e -> {
73+
if (e.getTestCaseId().equals(id)) {
74+
testData.add(new ArrayList<>() {{
75+
add(e);
76+
}});
77+
}
78+
});
79+
80+
return toArray(testData);
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
return new Object[0][0];
85+
}
86+
87+
private static Object[][] toArray(ArrayList<ArrayList<? extends BaseData>> data) {
88+
int noOfRows = data.size();
89+
Object[][] dataArray = new Object[noOfRows][1];
90+
91+
for (int i = 0; i < noOfRows; i++) {
92+
dataArray[i][0] = data.get(i).get(0);
93+
}
94+
95+
return dataArray;
96+
}
97+
6498
}
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[
2+
{
3+
"testCaseId": "TC-1",
4+
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
5+
"username": "standard_user",
6+
"password": "secret_sauce",
7+
"errorMessage": ""
8+
},
9+
{
10+
"testCaseId": "TC-1",
11+
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
12+
"username": "problem_user",
13+
"password": "secret_sauce",
14+
"errorMessage": ""
15+
},
16+
{
17+
"testCaseId": "TC-1",
18+
"testCaseDescription": "Correct username and correct password should redirect to 'Products' page",
19+
"username": "performance_glitch_user",
20+
"password": "secret_sauce",
21+
"errorMessage": ""
22+
},
23+
{
24+
"testCaseId": "TC-2",
25+
"testCaseDescription": "Incorrect username and correct password should produce errorMessage",
26+
"username": "username",
27+
"password": "secret_sauce",
28+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
29+
},
30+
{
31+
"testCaseId": "TC-2",
32+
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
33+
"username": "standard_user",
34+
"password": "password",
35+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
36+
},
37+
{
38+
"testCaseId": "TC-2",
39+
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
40+
"username": "locked_out_user",
41+
"password": "password",
42+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
43+
},
44+
{
45+
"testCaseId": "TC-2",
46+
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
47+
"username": "problem_user",
48+
"password": "password",
49+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
50+
},
51+
{
52+
"testCaseId": "TC-2",
53+
"testCaseDescription": "Correct username and incorrect password should produce errorMessage",
54+
"username": "performance_glitch_user",
55+
"password": "password",
56+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
57+
},
58+
{
59+
"testCaseId": "TC-2",
60+
"testCaseDescription": "Incorrect username and incorrect password should produce errorMessage",
61+
"username": "demo_username",
62+
"password": "demo_password",
63+
"errorMessage": "Epic sadface: Username and password do not match any user in this service"
64+
},
65+
{
66+
"testCaseId": "TC-2",
67+
"testCaseDescription": "Blank username should produce errorMessage",
68+
"username": "",
69+
"password": "demo_password",
70+
"errorMessage": "Epic sadface: Username is required"
71+
},
72+
{
73+
"testCaseId": "TC-2",
74+
"testCaseDescription": "Blank password should produce errorMessage",
75+
"username": "demo_username",
76+
"password": "",
77+
"errorMessage": "Epic sadface: Password is required"
78+
},
79+
{
80+
"testCaseId": "TC-2",
81+
"testCaseDescription": "Should produce errorMessage for locked out user",
82+
"username": "locked_out_user",
83+
"password": "secret_sauce",
84+
"errorMessage": "Epic sadface: Sorry, this user has been locked out."
85+
}
86+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"testCaseId": "TC-1",
4+
"testCaseDescription": "Logging out should redirect to login page",
5+
"username": "standard_user",
6+
"password": "secret_sauce",
7+
"url": "https://www.saucedemo.com/"
8+
}
9+
]

0 commit comments

Comments
 (0)