问题
I have Rest Assured-TestNG based automation suite and it uses Extent Reports. I need a way to make all the logs from Rest Assured also to be logged into Extent Reports.
I am using log4j for logging Rest Assured requests and responses into files and console, can these logs be feed into Extent Reports?
Is there any way to do it?
回答1:
Following sample code generates the Extent Reports and Logs the details. Please not you will need the following dependencies as well to successfully execute.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.6</version>
</dependency>
Make sure the above dependencies are updated in the pom
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import org.apache.commons.io.output.WriterOutputStream;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.response.Response;
/**
* @author vamsir
*
*/
public class SampleExtentReport {
static StringWriter requestWriter;
static PrintStream requestCapture;
static ExtentTest test;
static ExtentReports extentReports;
@BeforeSuite
public void init(ITestContext context) {
extentReports = new ExtentReports(System.getProperty("user.dir") + File.separator + "reports" + File.separator
+this.getClass().getSimpleName().toString() + ".html");
}
@Test
public static void sampleTest() {
test = extentReports.startTest("Get Sample Test");
requestWriter = new StringWriter();
requestCapture = new PrintStream(new WriterOutputStream(requestWriter));
Response response = RestAssured.given().filter(new RequestLoggingFilter(requestCapture)).and().baseUri("https://jsonplaceholder.typicode.com").and().basePath("/todos/1").when().get();
requestCapture.flush();
System.out.println("Request: "+requestWriter.toString());
System.out.println("Response: "+response.asString());
test.log(LogStatus.INFO, "Request : "+ requestWriter.toString());
test.log(LogStatus.INFO, "Response : " + response.asString());
extentReports.endTest(test);
}
@AfterMethod
public void getResult(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
test.log(LogStatus.FAIL, result.getThrowable());
}
}
@AfterSuite
public void end() {
extentReports.flush();
extentReports.close();
}
}
Here is the sample output I received using the above piece of code:
[RemoteTestNG] detected TestNG version 6.14.3
Request: Request method: GET
Request URI: https://jsonplaceholder.typicode.com/todos/1
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Cookies: <none>
Multiparts: <none>
Body: <none>
Response: {
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
PASSED: sampleTest
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
来源:https://stackoverflow.com/questions/46607347/how-to-log-restassured-request-and-response-information-to-extentreports-logs