Skip to content

Commit c2a17b7

Browse files
committed
refactor and improvements
1 parent 1c9e67c commit c2a17b7

File tree

14 files changed

+80
-68
lines changed

14 files changed

+80
-68
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ The project is structured as follows:
146146
- ### Test Data
147147
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.
148148
149-
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:
150-
149+
To add configurations for new test data, add a new Java bean in the [*data*](./src/main/java/io/github/tahanima/dto) 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:
150+
151151
```java
152-
package io.github.tahanima.data;
152+
package io.github.tahanima.dto;
153153
154154
import com.univocity.parsers.annotations.Parsed;
155155
@@ -186,7 +186,7 @@ The project is structured as follows:
186186
}
187187
]
188188
```
189-
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).
189+
For reference, check [this](./src/main/java/io/github/tahanima/dto/LoginData.java), [this](./src/test/resources/testdata/login.csv) and [this](./src/test/java/io/github/tahanima/e2e/LoginE2ETest.java).
190190

191191
- ### Browser
192192
The project contains the implementation of the *Chrome* and *Firefox* browsers. If you want to include an implementation of a new browser type, add the relevant codes in the [*BrowserFactory*](./src/main/java/io/github/tahanima/factory/BrowserFactory.java) enum.

build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

1717
dependencies {
18-
implementation 'org.seleniumhq.selenium:selenium-java:4.14.1'
18+
implementation 'org.seleniumhq.selenium:selenium-java:4.15.0'
1919
implementation 'org.aeonbits.owner:owner:1.0.12'
2020
implementation 'com.univocity:univocity-parsers:2.9.1'
2121
implementation 'com.aventstack:extentreports:5.1.1'
@@ -26,6 +26,8 @@ dependencies {
2626

2727
compileOnly 'org.projectlombok:lombok:1.18.30'
2828
annotationProcessor 'org.projectlombok:lombok:1.18.30'
29+
testCompileOnly 'org.projectlombok:lombok:1.18.30'
30+
testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'
2931

3032
testImplementation 'org.testng:testng:7.8.0'
3133
testImplementation 'org.assertj:assertj-core:3.24.2'

src/main/java/io/github/tahanima/data/BaseData.java renamed to src/main/java/io/github/tahanima/dto/BaseDto.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.tahanima.data;
1+
package io.github.tahanima.dto;
22

33
import com.univocity.parsers.annotations.Parsed;
44

@@ -10,7 +10,7 @@
1010
*/
1111
@Getter
1212
@ToString
13-
public class BaseData {
13+
public class BaseDto {
1414

1515
@Parsed(field = "Test Case ID", defaultNullRead = "")
1616
private String testCaseId;

src/main/java/io/github/tahanima/data/LoginData.java renamed to src/main/java/io/github/tahanima/dto/LoginDto.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.tahanima.data;
1+
package io.github.tahanima.dto;
22

33
import com.univocity.parsers.annotations.Parsed;
44

@@ -10,7 +10,7 @@
1010
*/
1111
@Getter
1212
@ToString(callSuper = true)
13-
public class LoginData extends BaseData {
13+
public class LoginDto extends BaseDto {
1414

1515
@Parsed(field = "Username", defaultNullRead = "")
1616
private String username;

src/main/java/io/github/tahanima/data/ProductsData.java renamed to src/main/java/io/github/tahanima/dto/ProductsDto.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.tahanima.data;
1+
package io.github.tahanima.dto;
22

33
import com.univocity.parsers.annotations.Parsed;
44

@@ -10,7 +10,7 @@
1010
*/
1111
@Getter
1212
@ToString(callSuper = true)
13-
public final class ProductsData extends BaseData {
13+
public final class ProductsDto extends BaseDto {
1414

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

src/main/java/io/github/tahanima/factory/BasePageFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import io.github.tahanima.ui.page.BasePage;
44

5+
import lombok.extern.slf4j.Slf4j;
56
import org.openqa.selenium.WebDriver;
67

78
/**
89
* @author tahanima
910
*/
11+
@Slf4j
1012
public final class BasePageFactory {
1113

1214
private BasePageFactory() {}
@@ -21,7 +23,7 @@ public static <T extends BasePage> T createInstance(
2123

2224
return clazz.cast(instance);
2325
} catch (Exception e) {
24-
e.printStackTrace();
26+
log.error("BasePageFactory::createInstance", e);
2527
}
2628

2729
throw new NullPointerException("Page class instantiation failed.");

src/main/java/io/github/tahanima/report/ExtentReportManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.text.SimpleDateFormat;
1111
import java.util.Date;
12+
import java.util.Objects;
1213

1314
/**
1415
* @author tahanima
@@ -36,7 +37,7 @@ public static ExtentReports createReport() {
3637
extentReport.setSystemInfo(
3738
"Test Group",
3839
StringUtils.capitalize(
39-
StringUtils.defaultString(System.getProperty("groups"), "regression")));
40+
Objects.toString(System.getProperty("groups"), "regression")));
4041

4142
return extentReport;
4243
}

src/main/java/io/github/tahanima/ui/component/BaseComponent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public abstract class BaseComponent {
99

1010
protected WebDriver driver;
1111

12-
public BaseComponent(WebDriver driver) {
12+
protected BaseComponent(WebDriver driver) {
1313
this.driver = driver;
1414
}
1515
}

src/main/java/io/github/tahanima/ui/page/LoginPage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public LoginPage open() {
2828
return this;
2929
}
3030

31-
private void clearAndType(WebElement elem, String text) {
31+
private void clearAndType(final WebElement elem, final String text) {
3232
elem.clear();
3333
elem.sendKeys(text);
3434
}

src/test/java/io/github/tahanima/e2e/BaseE2ETest.java renamed to src/test/java/io/github/tahanima/e2e/BaseTest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author tahanima
1717
*/
1818
@Listeners(TestListener.class)
19-
public abstract class BaseE2ETest {
19+
public abstract class BaseTest {
2020

2121
private final WebDriver driver =
2222
BrowserFactory.valueOf(config().browser().toUpperCase()).getDriver();
@@ -30,10 +30,6 @@ protected String getScreenshotFilePath(String path) {
3030
return config().baseScreenshotPath() + path;
3131
}
3232

33-
public WebDriver getDriver() {
34-
return driver;
35-
}
36-
3733
@BeforeClass(alwaysRun = true)
3834
public void setup() {
3935
loginPage = BasePageFactory.createInstance(driver, LoginPage.class);

src/test/java/io/github/tahanima/e2e/LoginE2ETest.java renamed to src/test/java/io/github/tahanima/e2e/LoginTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
import io.github.tahanima.data.LoginData;
7+
import io.github.tahanima.dto.LoginDto;
88
import io.github.tahanima.ui.page.ProductsPage;
99
import io.github.tahanima.util.TestRetry;
1010

@@ -19,15 +19,15 @@
1919
/**
2020
* @author tahanima
2121
*/
22-
public final class LoginE2ETest extends BaseE2ETest {
22+
public final class LoginTest extends BaseTest {
2323

2424
private static final String FILE_PATH = "login.csv";
2525

2626
@DataProvider(name = "loginData")
2727
public Object[][] getLoginData(final Method testMethod) {
2828
String testCaseId = testMethod.getAnnotation(Test.class).testName();
2929

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

3333
@AfterMethod(alwaysRun = true)
@@ -49,7 +49,7 @@ public void captureScreenshotOnFailure(ITestResult result) {
4949
dataProvider = "loginData",
5050
groups = {"smoke", "regression"},
5151
retryAnalyzer = TestRetry.class)
52-
public void testCorrectUserNameAndCorrectPassword(final LoginData data) {
52+
public void testCorrectUserNameAndCorrectPassword(final LoginDto data) {
5353
ProductsPage productsPage = loginPage.loginAs(data.getUsername(), data.getPassword());
5454

5555
assertThat(productsPage.getTitle()).isEqualTo("Products");
@@ -60,7 +60,7 @@ public void testCorrectUserNameAndCorrectPassword(final LoginData data) {
6060
dataProvider = "loginData",
6161
groups = {"validation", "regression"},
6262
retryAnalyzer = TestRetry.class)
63-
public void testImproperCredentialsShouldGiveErrorMessage(final LoginData data) {
63+
public void testImproperCredentialsShouldGiveErrorMessage(final LoginDto data) {
6464
loginPage.loginAs(data.getUsername(), data.getPassword());
6565

6666
assertThat(loginPage.getErrorMessage()).isEqualTo(data.getErrorMessage());

src/test/java/io/github/tahanima/e2e/ProductsE2ETest.java renamed to src/test/java/io/github/tahanima/e2e/ProductsTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
import io.github.tahanima.data.ProductsData;
7+
import io.github.tahanima.dto.ProductsDto;
88
import io.github.tahanima.util.TestRetry;
99

1010
import org.testng.ITestNGMethod;
@@ -18,15 +18,15 @@
1818
/**
1919
* @author tahanima
2020
*/
21-
public final class ProductsE2ETest extends BaseE2ETest {
21+
public final class ProductsTest extends BaseTest {
2222

23-
private static final String FILE_PATH = "products.csv";
23+
private static final String FILE_PATH = "products.json";
2424

2525
@DataProvider(name = "productsData")
2626
public Object[][] getProductsData(final Method testMethod) {
2727
String testCaseId = testMethod.getAnnotation(Test.class).testName();
2828

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

3232
@AfterMethod(alwaysRun = true)
@@ -48,7 +48,7 @@ public void captureScreenshotOnFailure(ITestResult result) {
4848
dataProvider = "productsData",
4949
groups = {"smoke", "regression"},
5050
retryAnalyzer = TestRetry.class)
51-
public void testSuccessfulLogout(final ProductsData data) {
51+
public void testSuccessfulLogout(final ProductsDto data) {
5252
loginPage.loginAs(data.getUsername(), data.getPassword()).clickOnLogout();
5353

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

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

+45-34
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
import com.google.gson.reflect.TypeToken;
55
import com.univocity.parsers.csv.CsvParserSettings;
66
import com.univocity.parsers.csv.CsvRoutines;
7-
import io.github.tahanima.data.BaseData;
7+
8+
import io.github.tahanima.dto.BaseDto;
9+
10+
import lombok.extern.slf4j.Slf4j;
811

912
import java.io.FileInputStream;
1013
import java.io.IOException;
1114
import java.io.InputStreamReader;
12-
import java.io.Reader;
1315
import java.nio.charset.StandardCharsets;
1416
import java.util.ArrayList;
1517
import java.util.List;
1618

1719
/**
1820
* @author tahanima
1921
*/
22+
@Slf4j
2023
public final class DataProviderUtil {
2124

2225
private DataProviderUtil() {}
2326

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);
29-
}
27+
public static Object[][] processTestData(
28+
Class<? extends BaseDto> clazz, String fileName, String id) {
29+
if (fileName.endsWith(".csv")) return processCsv(clazz, fileName, id);
30+
31+
if (fileName.endsWith(".json")) return processJson(clazz, fileName, id);
3032

3133
return new Object[0][0];
3234
}
3335

34-
private static Object[][] processCsv(Class<? extends BaseData> clazz, String fileName, String id) {
35-
CsvParserSettings settings = new CsvParserSettings();
36+
private static Object[][] processCsv(
37+
Class<? extends BaseDto> clazz, String fileName, String id) {
38+
var settings = new CsvParserSettings();
3639

3740
settings.getFormat().setLineSeparator("\n");
3841

39-
CsvRoutines routines = new CsvRoutines(settings);
42+
var routines = new CsvRoutines(settings);
4043

41-
try (Reader reader =
44+
try (var reader =
4245
new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8)) {
43-
ArrayList<ArrayList<? extends BaseData>> testData = new ArrayList<>();
46+
ArrayList<ArrayList<? extends BaseDto>> testData = new ArrayList<>();
4447

4548
routines.iterate(clazz, reader)
4649
.forEach(
@@ -57,42 +60,50 @@ private static Object[][] processCsv(Class<? extends BaseData> clazz, String fil
5760

5861
return toArray(testData);
5962
} catch (IOException e) {
60-
e.printStackTrace();
63+
log.error("DataProviderUtil::processCsv", e);
6164
}
6265

6366
return new Object[0][0];
6467
}
6568

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-
});
69+
private static <T extends BaseDto> Object[][] processJson(
70+
Class<T> clazz, String fileName, String id) {
71+
try (var reader =
72+
new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8)) {
73+
ArrayList<ArrayList<? extends BaseDto>> testData = new ArrayList<>();
74+
List<T> jsonData =
75+
new Gson()
76+
.fromJson(
77+
reader,
78+
TypeToken.getParameterized(List.class, clazz).getType());
79+
80+
jsonData.forEach(
81+
e -> {
82+
if (e.getTestCaseId().equals(id)) {
83+
testData.add(
84+
new ArrayList<>() {
85+
{
86+
add(e);
87+
}
88+
});
89+
}
90+
});
7991

8092
return toArray(testData);
8193
} catch (IOException e) {
82-
e.printStackTrace();
94+
log.error("DataProviderUtil::processJson", e);
8395
}
8496
return new Object[0][0];
8597
}
8698

87-
private static Object[][] toArray(ArrayList<ArrayList<? extends BaseData>> data) {
88-
int noOfRows = data.size();
89-
Object[][] dataArray = new Object[noOfRows][1];
99+
private static Object[][] toArray(ArrayList<ArrayList<? extends BaseDto>> testData) {
100+
int noOfRows = testData.size();
101+
Object[][] testDataArray = new Object[noOfRows][1];
90102

91103
for (int i = 0; i < noOfRows; i++) {
92-
dataArray[i][0] = data.get(i).get(0);
104+
testDataArray[i][0] = testData.get(i).get(0);
93105
}
94106

95-
return dataArray;
107+
return testDataArray;
96108
}
97-
98109
}

0 commit comments

Comments
 (0)