如何获取要在案例.write()中写入的REST保证日志?

时间:2019-02-27 14:26:28

标签: java rest cucumber rest-assured cucumber-java

我需要记录放心的流量-使用以下方法进行记录:when().post().then().log().all().extract().response();
现在,将其余消息放入标准输出中,但我希望这些消息通过AfterAll()scenario.write()调用中记录在黄瓜报告中。
如何将放心的日志保存到变量中并将其传递给scenario.write()

2 个答案:

答案 0 :(得分:1)

您可以使用RequestLoggingFilterResponseLoggingFilter类。将配置的PrintStream提供给其构造函数,并添加过滤器以请求规范。它可以这样看:

OutputStream outputStream = null; //use your OutputStream that will write where you need it

PrintStream printStream = new PrintStream(outputStream, true);

RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(printStream);
ResponseLoggingFilter responseLoggingFilter = new ResponseLoggingFilter(printStream);

RestAssured.given()
    .filters(requestLoggingFilter, responseLoggingFilter)
    .when()
    .get("/api");

或者您也可以实现Filter接口以使用自己的接口。 这是记录请求和响应的示例:

public class CustomLogFilter implements Filter {

  Logger log = LoggerFactory.getLogger(CustomLogFilter.class);

  @Override
  public Response filter(FilterableRequestSpecification requestSpec,
      FilterableResponseSpecification responseSpec, FilterContext ctx) {
    Response response = ctx.next(requestSpec, responseSpec);
    StringBuilder requestBuilder = new StringBuilder();
    requestBuilder.append(requestSpec.getMethod());
    requestBuilder.append("\n");
    requestBuilder.append(requestSpec.getURI());
    requestBuilder.append("\n");
    requestBuilder.append("*************");
    log.info(requestBuilder.toString());  //Log your request where you need it
    StringBuilder responseBuilder = new StringBuilder();
    responseBuilder.append(response.getStatusLine());
    responseBuilder.append("\n");
    responseBuilder.append(response.getBody());
    log.info(responseBuilder.toString()); //Log your response where you need it
    return response;
  }
}

然后在RestAssured请求中使用它:

Filter logFilter = new CustomLogFilter();
RestAssured.given()
    .filter(logFilter)
    .when()
    .get("/api");

答案 1 :(得分:0)

我们需要先将Response转换为String,然后在日志或censage.write中使用此字符串引用。我试图在下面的代码中解释-

    String responseString, placeid;
    private static Logger log =LogManager.getLogger(basics3.class.getName());
        Properties prop=new Properties();
        @Before
        public void getData() throws IOException
        {
            FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+"//env.properties");
            prop.load(fis);
        }

        @Test
        public void AddandDeletePlace()
        {

            RestAssured.baseURI= prop.getProperty("HOST");
            Response res=given().

            queryParam("key",prop.getProperty("KEY")).
            body(payLoad.getPostData()).
            when().
            post(resources1.placePostData()).
            then().assertThat().statusCode(200).and().contentType(ContentType.JSON).and().
            body("status",equalTo("OK")).
            extract().response();

            responseString=res.asString();
            JsonPath js= new JsonPath(responseString);
            placeid=js.get("place_id");
        }

         @After
         public void setLogs(){
         log.info(responseString);          
         log.info(placeid);
         } 

希望这使您对打印REST保证日志有一定的了解。