Skip to content

Commit 2063327

Browse files
committed
Added capability to execute test cases according to requirement from
RunSetup.csv. Added new class, GloblaConstants for constants.
1 parent f3cec19 commit 2063327

File tree

12 files changed

+160
-184
lines changed

12 files changed

+160
-184
lines changed

WebTestRunner.xml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3-
<suite verbose="1" name="Demo test suite" parallel="false" thread-count="1" >
3+
<suite name="Demo test suite" >
44

55

66
<listeners>
7-
<listener class-name="listeners.TestngListener" ></listener>
7+
<listener class-name="listeners.TestListener" ></listener>
8+
<listener class-name="listeners.MethodInterceptor" ></listener>
9+
810
</listeners>
911

1012
<parameter name = "browser" value = "chrome"></parameter>
11-
<test name="Navigate to home page">
13+
<test name="Navigate to home page" parallel="tests" thread-count="1">
1214

1315
<classes>
14-
<class name="Tests.NewUserRegistration">
15-
<methods>
16-
<include name="navigateToHomepage"></include>
17-
</methods>
18-
</class>
16+
<class name="Tests.NewUserRegistration"/>
1917
</classes>
2018
</test>
21-
19+
<!--
2220
<parameter name = "browser" value = "chrome"></parameter>
23-
<test name="Navigate to user signup page">
21+
<test name="Navigate to user signup page" parallel="tests" thread-count="1">
2422
<classes>
2523
<class name="Tests.NewUserRegistration">
2624
<methods>
@@ -31,7 +29,7 @@
3129
</test>
3230
3331
<parameter name = "browser" value = "chrome"></parameter>
34-
<test name="Register new user">
32+
<test name="Register new user" parallel="tests" thread-count="1">
3533
<classes>
3634
<class name="Tests.NewUserRegistration">
3735
<methods>
@@ -40,5 +38,5 @@
4038
</class>
4139
</classes>
4240
</test>
43-
41+
-->
4442
</suite>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package globalconstants;
2+
3+
public class GlobalConstants {
4+
public static final String RUN_SETUP_CSV = System.getProperty("user.dir")+"/src/test/resources/RunSetup.csv";
5+
6+
}

src/main/java/listeners/ExtentReportGenerator.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package listeners;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.testng.IMethodInstance;
10+
import org.testng.IMethodInterceptor;
11+
import org.testng.ITestContext;
12+
13+
import globalconstants.GlobalConstants;
14+
import utilities.filereaders.CsvReaderUtility;
15+
16+
/*
17+
* TestNG listener to get the list of tests to run.
18+
*/
19+
public class MethodInterceptor implements IMethodInterceptor{
20+
21+
List<IMethodInstance> testList = new ArrayList<>();
22+
23+
// Method will be executed only once for the entire test suite
24+
// Returns the list of test cases to run according to the execute parameter in csv file
25+
@Override
26+
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
27+
28+
List<Map<String, String>> mapList = new ArrayList<>();
29+
30+
try {
31+
mapList.addAll(CsvReaderUtility.read(new File(GlobalConstants.RUN_SETUP_CSV)));
32+
testList=getListOfTestCasesToRun(methods, mapList);
33+
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
38+
return testList;
39+
}
40+
41+
//Method to get the list of test cases to run
42+
private List<IMethodInstance> getListOfTestCasesToRun(List<IMethodInstance> methods, List<Map<String, String>> mapList){
43+
for(var method : methods){
44+
for(var map : mapList){
45+
if(method.getMethod().getMethodName().equalsIgnoreCase(map.get("name"))) {
46+
if(map.get("execute").equalsIgnoreCase("true")) {
47+
method.getMethod().setDescription(map.get("description"));
48+
testList.add(method);
49+
}
50+
}
51+
}
52+
}
53+
return testList;
54+
}
55+
}

src/main/java/listeners/TestngListener.java renamed to src/main/java/listeners/TestListener.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package listeners;
22

3+
import java.awt.Desktop;
34
import java.io.File;
45
import java.io.IOException;
6+
import java.util.Arrays;
57

68
import org.openqa.selenium.io.FileHandler;
79
import org.testng.ISuite;
@@ -16,22 +18,23 @@
1618

1719
import decorators.Driver;
1820
import logsetup.Log;
21+
import reports.ExtentReport;
1922

20-
public class TestngListener implements ITestListener, ISuiteListener{
23+
public class TestListener implements ITestListener, ISuiteListener{
2124
ExtentReports extentReport;
2225
ExtentTest extentTest ;
2326

2427
@Override
2528
public void onStart(ISuite suite) {
2629
//To generate extend report at the start of the suite execution
27-
extentReport=ExtentReportGenerator.generateReport();
30+
extentReport=ExtentReport.initReport();
2831
Log.info("\""+suite.getName()+ "\" test suite execution started.");
2932
}
3033

3134
@Override
3235
public void onFinish(ISuite suite) {
3336
//Flush is used to create the extend report
34-
extentReport.flush();
37+
extentReport.flush();
3538
Log.info("\""+suite.getName()+ "\" test suite execution ended.");
3639
}
3740

@@ -42,31 +45,31 @@ public void onStart(ITestContext context) {
4245

4346
@Override
4447
public void onTestStart(ITestResult result) {
45-
String testName=result.getName();
46-
extentTest = extentReport.createTest(testName);
47-
extentTest.log(Status.INFO, testName+" started." );
48-
Log.info("\""+testName+"\" execution started. EntentTest created");
48+
String testDesc=result.getMethod().getDescription();
49+
extentTest = extentReport.createTest(testDesc);
50+
extentTest.log(Status.INFO, testDesc+" started." );
51+
Log.info("\""+testDesc+"\" execution started. EntentTest created");
4952

5053
}
5154

5255
@Override
5356
public void onTestSuccess(ITestResult result) {
54-
String testName=result.getName();
55-
extentTest.log(Status.PASS, testName+" test passed." );
56-
Log.info("\""+testName+"\" passed.");
57+
String testDesc=result.getMethod().getDescription();
58+
extentTest.log(Status.PASS, testDesc+" test passed." );
59+
Log.info("\""+testDesc+"\" passed.");
5760

5861
}
5962

6063
@Override
6164
public void onTestFailure(ITestResult result) {
62-
String testName=result.getName();
65+
String testDesc=result.getMethod().getDescription();
6366

6467
//Getting the driver from result parameter for taking the screenshot on failure of test
6568
try {
6669
Driver driver = (Driver)result.getTestClass().getRealClass().getField("driver").get(result.getInstance());
6770

6871
//Take screenshot and copying to Screenshot folder in the project
69-
String screenshotsFolderPath =System.getProperty("user.dir")+"/Screenshots/"+testName+".png";
72+
String screenshotsFolderPath =System.getProperty("user.dir")+"/Screenshots/"+testDesc+".png";
7073

7174
//Call takescreenshot() method from DriverLogger class and copying the screenshot from source path to Screenshot folder
7275
FileHandler.copy(driver.takescreenshot(), new File(screenshotsFolderPath));
@@ -78,14 +81,16 @@ public void onTestFailure(ITestResult result) {
7881
e.printStackTrace();
7982
}
8083

81-
extentTest.log(Status.FAIL, testName +" failed. \n"+result.getThrowable());
84+
extentTest.log(Status.FAIL, testDesc +" failed.");
85+
extentTest.fail(Arrays.toString(result.getThrowable().getStackTrace()));//beautify stacktrace
8286
Log.error("\""+result.getName()+"\" failed.", result.getThrowable());
8387
}
8488

8589
@Override
8690
public void onTestSkipped(ITestResult result) {
8791
String testName=result.getName();
88-
extentTest.log(Status.SKIP, testName +" skipped." + result.getThrowable());
92+
extentTest.log(Status.SKIP, testName +" skipped.");
93+
extentTest.skip(Arrays.toString(result.getThrowable().getStackTrace()));
8994
Log.error("\""+result.getName()+"\" skipped.", result.getThrowable());
9095
}
9196

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package reports;
2+
3+
import com.aventstack.extentreports.ExtentTest;
4+
5+
public class ExtentManager {
6+
7+
private ExtentManager() {} //Cannot invoke the constructor from other methods
8+
9+
//To make the extent report thread safe, Thread local is used
10+
private static ThreadLocal<ExtentTest> tlExtentTest = new ThreadLocal<>();
11+
12+
public static ExtentTest GetExtentTest() {
13+
return tlExtentTest.get();
14+
}
15+
16+
//Method to set the threadlocal version of extend report
17+
public static void setExtentTest(ExtentTest test) {
18+
tlExtentTest.set(test);
19+
}
20+
21+
public static void unload() {
22+
tlExtentTest.remove();
23+
}
24+
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utilities.filereaders;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.MappingIterator;
11+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
12+
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
13+
14+
public final class CsvReaderUtility {
15+
16+
private CsvReaderUtility() {}
17+
18+
public static List<Map<String, String>> read(File file) throws JsonProcessingException, IOException {
19+
List<Map<String, String>> response = new LinkedList<Map<String, String>>();
20+
CsvMapper mapper = new CsvMapper();
21+
CsvSchema schema = CsvSchema.emptySchema().withHeader().withColumnSeparator(',').withComments();
22+
MappingIterator<Map<String, String>> iterator = mapper.reader(Map.class)
23+
.with(schema)
24+
.readValues(file);
25+
26+
while (iterator.hasNext()) {
27+
response.add(iterator.next());
28+
}
29+
return response;
30+
}
31+
32+
33+
}

src/main/java/utilities/filereaders/RunSetupUtility.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)