I want to log request data which gets printed in console to testNG report file using testNG's Reporter.log() method which expects string input. Below is my request specification:
private RequestSpecification getRequestSpec(ContentType requestbodytype, ContentType responsetype) {
return RestAssured.given().log().everything().contentType(requestbodytype).accept(responsetype);
}
protected Response get(String resourceURI, ContentType requestbodytype, ContentType responsetype, boolean enableUrlEncoding) {
Response rs = null;
rs = getRequestSpec(requestbodytype, responsetype).when().get(resourceURI);
Reporter.log(rs.print());
return rs;
}
I want to log below content to testing log file along with response which I will receive upon exection of this call
Request method: GET
Request URI: http://localhost:port/url
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=application/json, application/javascript, text/javascript
Content-Type=application/json; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body: <none>
Can anyone please help me to resolve this.
Thanks
您可以将过滤器添加到requestSpecBuilder并向其添加Reporter.log。
rSpecBuilder.addFilter( (req, response, ctx) -> {Reporter.log("Req called" + req.getDerivedPath()); return ctx.next(req, response); });
I used RestAssured.config().logConfig()
and it worked fine. Now next challenge is to enable pretty print.
private RequestSpecification getRequestSpec(ContentType requestbodytype, ContentType responsetype, boolean urlEncodingEnabled) {
writer = new StringWriter();
captor = new PrintStream(new WriterOutputStream(writer), true);
return RestAssured.given().config(RestAssured.config().logConfig(new LogConfig(captor, true))).urlEncodingEnabled(urlEncodingEnabled).log().everything().contentType(requestbodytype).accept(responsetype);
}
protected Response get(String resourceURI, ContentType requestbodytype, ContentType responsetype, boolean urlEncodingEnabled) {
Response rs = null;
rs = getRequestSpec(requestbodytype, responsetype, urlEncodingEnabled).when().get(resourceURI);
Reporter.log("---- Request ----");
Reporter.log(writer.toString());
Reporter.log("---- Response ----");
Reporter.log(rs.asString());
return rs;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.