2017-10-13 65 views
0

我想使用testNG的Reporter.log()方法(它需要字符串輸入)將要打印到控制檯中的請求數據記錄到testNG報告文件。下面是我的要求規格:如何將重新審覈的請求數據記錄到testNG報告文件

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; 
} 

我要登錄下面的內容,以測試日誌文件與響應一起,我會收到在此調用exection

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> 

誰能幫我解決這個。

感謝

回答

0

我用RestAssured.config().logConfig()它工作得很好。現在接下來的挑戰是啓用漂亮的打印。

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; 
} 
0

您可以將過濾器添加到您的requestSpecBuilder並添加Reporter.log它。

rSpecBuilder.addFilter((req, response, ctx) -> {Reporter.log("Req called" + req.getDerivedPath()); return ctx.next(req, response); });

+0

我很抱歉,這些lambda表達式在您的答案?我只能使用Java 1.7.0_45。 – user1634358