java - 如何使用 Testng 框架将放心的控制台日志写入可编辑或 txt 文件?

标签 java rest-assured

我是 Java 新手,请放心,我无法在脚本中添加将控制台日志打印到可读文件的方法。我尝试使用 Log4j 但无法正确实现它。我在研究时发现了这一点,但我不确定如何实现 - https://static.javadoc.io/com.jayway.restassured/rest-assured/2.7.0/com/jayway/restassured/config/LogConfig.html

import static io.restassured.RestAssured.given;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import files.reusableFunctions;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;

public class Playlist_Acknowledgement {

    Properties prop = new Properties(); // creating prop object as global

    @BeforeTest
    public void testData1() throws Exception {

        FileInputStream f = new FileInputStream("D:\\Tools\\Workspace\\BXF\\src\\files\\config.properties");
        prop.load(f); // to load the file object into prop file

        reusableFunctions rf = new reusableFunctions();
        rf.createBxfPlaylistXml();
    }

    @SuppressWarnings("unused")
    @Test
    public void postData() throws IOException {


        String pth = prop.getProperty("PLAYLISTACK_ENDPOINT");
        File path = new File(pth);

        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {

            pth = files[i].toString();
            System.out.println("Path = " + pth);
            String postData = new String(Files.readAllBytes(Paths.get(pth)));
            // BaseURL
            // value populating from property above
            RestAssured.baseURI = prop.getProperty("AISHOST");

            Response resp = given().log().all()
                    .header("Content-Type", "application/XML; charset=utf-8")
                    .body(postData)
                    .when().post("/bxfxml")
                    .then().log().all()
                    .assertThat()
                    .statusCode(200).and()
                    .contentType(ContentType.XML)
                    .extract().response();


            // to convert raw data to string
            XmlPath xmlResponse = reusableFunctions.rawToXML(resp);
            String responseString = resp.asString();
            System.out.println("XML response is - " + responseString);
            // Files.delete(files[i].toPath());
            // Files.copy(path.toPath(), prop.getProperty(key) );
        }
    }
}

最佳答案

您可以按如下方式设置配置

LogConfig logconfig = new LogConfig().enableLoggingOfRequestAndResponseIfValidationFails().enablePrettyPrinting(true);
RestAssured.config().logConfig(logconfig);

之后您就可以继续常规操作

RestAssured.given().log().all()
        .header("Content-Type", "application/XML; charset=utf-8")
        .body("")
        .when().post("/bxfxml")
        .then().log().all()
        .assertThat()
        .statusCode(200).and()
        .contentType(ContentType.XML)
        .extract().response();

关于java - 如何使用 Testng 框架将放心的控制台日志写入可编辑或 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081913/

相关文章:

java - Parquet:递归 Thrift 数据结构

java - LibGdx 平滑背景颜色变化

java - System.out.println( '3' + 0 );

java - 如何检查 JSONArray 中特定的键和值是否可用

java - 请放心 json-schema-validator

java - 如何使用放心调用web服务

java - 使用 SOAP UI 而不是 REST-assured 来自动化 CI 的 REST 服务是否有优势

java - Google 如何验证 API 调用中的 SHA1 和程序包名称?

java - 无法使用 spring security 登录 Rest API

json - Grails 3.1.8 : Spring Rest Docs using Rest Assured giving PayloadHandlingException: com. fastxml.jackson.databind.JsonMappingException