我是 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/