Skip to content

Commit bcd1d31

Browse files
committed
Enhanced Report
1 parent 12e89aa commit bcd1d31

18 files changed

Lines changed: 609 additions & 130 deletions

Reports/firstReport.html

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
<version>20180130</version>
4646
</dependency>
4747

48+
<dependency>
49+
<groupId>com.aventstack</groupId>
50+
<artifactId>extentreports</artifactId>
51+
<version>3.1.5</version>
52+
</dependency>
4853

4954
</dependencies>
5055
</project>

src/test/java/javaTest/ExcelOperation.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1111
import org.apache.poi.ss.usermodel.Workbook;
1212

13+
import com.aventstack.extentreports.ExtentReports;
14+
1315
import io.restassured.response.Response;
1416

1517
public class ExcelOperation {
1618

17-
public static void readExcel(String filePath, String fileName, String sheetName) throws IOException {
19+
public static void readExcel(String filePath, String fileName, String sheetName, ExtentReports extent) throws IOException {
1820

19-
//initializing vars
2021
File file = new File(filePath + "\\" + fileName);
2122
FileInputStream inputStream = new FileInputStream(file);
2223

@@ -38,9 +39,8 @@ public static void readExcel(String filePath, String fileName, String sheetName)
3839

3940
System.out.println("Processing "+i+") " +reqUrl+" "+method);
4041

41-
//call to method which sends request
4242
if(sendRequest.testResponseCode(reqUrl,sheet.getRow(i).getCell(2).toString(),Double.parseDouble(sheet.getRow(i).getCell(3).toString()),
43-
sheet.getRow(i).getCell(5).toString())==0) {
43+
sheet.getRow(i).getCell(5).toString(),Integer.valueOf((int) Math.round(Double.parseDouble(sheet.getRow(i).getCell(6).toString()))),extent)==0) {
4444
FileOutputStream f2 = new FileOutputStream(file);
4545
HSSFCell cell = sheet.getRow(i).createCell(4);
4646
cell.setCellValue("PASS");
@@ -59,6 +59,7 @@ public static void readExcel(String filePath, String fileName, String sheetName)
5959
System.out.println("******************************************************");
6060
}
6161
}
62+
extent.flush();
6263
}
6364
}
6465
}

src/test/java/javaTest/JavaTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
import java.io.IOException;
44

5-
import org.testng.annotations.Test;
5+
import com.aventstack.extentreports.ExtentReports;
6+
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
67

7-
// framework to test whether any api call fail to response for what it is made.
88
public class JavaTest {
99

1010
public ExcelOperation excelOp;
1111

12-
//TestNg will analyze for this annotation and execute script from here
13-
@Test
14-
public void testResponseCode() throws IOException {
15-
16-
//initializing imp variables
12+
13+
public static void main(String args[]) throws IOException {
1714
String filePath = "C:\\API-Test-Excel";
1815
String fileName = "Test-Api.xls";
1916
String sheetName = "Sheet1";
2017

21-
ExcelOperation.readExcel(filePath, fileName, sheetName);
22-
18+
ExtentHtmlReporter reporter = new ExtentHtmlReporter("./Reports/firstReport.html");
19+
ExtentReports extent = new ExtentReports();
20+
extent.attachReporter(reporter);
21+
22+
ExcelOperation.readExcel(filePath, fileName, sheetName, extent);
23+
2324
}
25+
26+
27+
2428
}

src/test/java/javaTest/sendRequest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
import org.json.JSONObject;
66
import org.testng.annotations.Test;
77

8+
import com.aventstack.extentreports.ExtentReports;
9+
import com.aventstack.extentreports.ExtentTest;
10+
import com.aventstack.extentreports.Status;
11+
812
import io.restassured.RestAssured;
913
import io.restassured.response.Response;
1014
import io.restassured.specification.RequestSpecification;
1115

1216
public class sendRequest {
1317

1418
@Test
15-
public static int testResponseCode(String reqUrl, String methodName, double expectedCode, String jsonBody) {
19+
public static int testResponseCode(String reqUrl, String methodName, double expectedCode1, String jsonBody,
20+
int expectedTime, ExtentReports extent) {
1621
System.out.println("method : " + methodName);
22+
int expectedCode = Integer.valueOf((int) Math.round(expectedCode1));
1723
System.out.println("expected code is :- " + expectedCode);
1824
if (!jsonBody.contains("NA")) {
1925
JSONObject jsonobj = new JSONObject(jsonBody);
@@ -23,32 +29,53 @@ public static int testResponseCode(String reqUrl, String methodName, double expe
2329
RestAssured.baseURI = reqUrl;
2430
RequestSpecification request = RestAssured.given();
2531

32+
ExtentTest logger2 = extent.createTest(methodName + " " + reqUrl);
33+
2634
if (methodName.equalsIgnoreCase("get")) {
2735
resp = get(reqUrl);
36+
code = resp.getStatusCode();
2837
} else if (methodName.equalsIgnoreCase("post")) {
2938

3039
request.header("Content-Type", "application/json");
3140
resp = request.post();
41+
code = resp.getStatusCode();
3242

3343
} else if (methodName.equalsIgnoreCase("put")) {
3444

3545
request.header("Content-Type", "application/json");
3646
resp = request.put();
47+
code = resp.getStatusCode();
3748

3849
} else if (methodName.equalsIgnoreCase("delete")) {
3950

4051
request.header("Content-Type", "application/json");
4152
resp = request.delete();
42-
53+
code = resp.getStatusCode();
4354
}
4455
System.out.println(methodName + " method :" + code);
4556
if (resp != null) {
4657
code = resp.getStatusCode();
4758
}
4859

4960
if (code == Math.round(expectedCode)) {
61+
if (resp.getTime() > expectedTime) {
62+
logger2.log(Status.FAIL,
63+
"<b>Faild reason: </b>Response Time is greater than Expected Time.<br />"
64+
+ "<b>Expected code:</b> " + expectedCode + "<br /><b>Actual response:</b> " + code
65+
+ "<br />" + "<b>Response time: </b>" + resp.getTime() + "<br /><b>Expected time: </b>"
66+
+ expectedTime + "<br><b>Response Body: </b>" + resp.asString());
67+
return -1;
68+
}
69+
logger2.log(Status.PASS,
70+
"<b>Expected code:</b> " + expectedCode + "<br /><b>Actual response:</b> " + code + "<br />"
71+
+ "<b>Response time: </b>" + resp.getTime() + "<b><br />Expected time: </b>" + expectedTime
72+
+ "<br />Response Body: " + resp.asString());
5073
return 0;
5174
} else {
75+
logger2.log(Status.FAIL,
76+
"<b>Expected code:</b> " + expectedCode + "<br /><b>Actual response:</b> " + code + "<br />"
77+
+ "<b>Response time: </b> " + resp.getTime() + "<br /><b>Response Body: </b>"
78+
+ resp.asString());
5279
return -1;
5380
}
5481

386 Bytes
Binary file not shown.
519 Bytes
Binary file not shown.
1.58 KB
Binary file not shown.

test-output/Default suite/Default test.html

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,17 @@
5555
<body>
5656
<h2 align='center'>Default test</h2><table border='1' align="center">
5757
<tr>
58-
<td>Tests passed/Failed/Skipped:</td><td>1/0/0</td>
58+
<td>Tests passed/Failed/Skipped:</td><td>0/0/0</td>
5959
</tr><tr>
60-
<td>Started on:</td><td>Wed Dec 19 01:27:02 IST 2018</td>
60+
<td>Started on:</td><td>Thu Dec 20 00:11:06 IST 2018</td>
6161
</tr>
62-
<tr><td>Total time:</td><td>12 seconds (12010 ms)</td>
62+
<tr><td>Total time:</td><td>0 seconds (16 ms)</td>
6363
</tr><tr>
6464
<td>Included groups:</td><td></td>
6565
</tr><tr>
6666
<td>Excluded groups:</td><td></td>
6767
</tr>
6868
</table><p/>
6969
<small><i>(Hover the method name to see the test class name)</i></small><p/>
70-
<table width='100%' border='1' class='invocation-passed'>
71-
<tr><td colspan='4' align='center'><b>PASSED TESTS</b></td></tr>
72-
<tr><td><b>Test method</b></td>
73-
<td width="30%"><b>Exception</b></td>
74-
<td width="10%"><b>Time (seconds)</b></td>
75-
<td><b>Instance</b></td>
76-
</tr>
77-
<tr>
78-
<td title='javaTest.JavaTest.testResponseCode()'><b>testResponseCode</b><br>Test class: javaTest.JavaTest</td>
79-
<td></td>
80-
<td>11</td>
81-
<td>javaTest.JavaTest@5419f379</td></tr>
82-
</table><p>
8370
</body>
8471
</html>
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!-- Generated by org.testng.reporters.JUnitXMLReporter -->
3-
<testsuite hostname="SAGAR" name="Default test" tests="1" failures="0" timestamp="18 Dec 2018 19:57:14 GMT" time="12.01" errors="0">
4-
<testcase name="testResponseCode" time="11.993" classname="javaTest.JavaTest"/>
5-
</testsuite> <!-- Default test -->
3+
<testsuite hostname="SAGAR" tests="0" failures="0" timestamp="19 Dec 2018 18:41:06 GMT" time="0.016" errors="0">
4+
</testsuite>

0 commit comments

Comments
 (0)