我需要从每个“保证放心”测试中获取“ CURL”操作,并在未通过测试时在控制台中进行打印。有可能吗?
对于轴,请转换:
given().relaxedHTTPSValidation().
auth().basic("name", "password").
param("grant_type","client_credentials").
when().
post("https://test_url/oauth/token").
then().
statusCode(200);
到
curl --user name:passwoed -k -X POST \
https://test_url/oauth/token \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-d grant_type=client_credentials
答案 0 :(得分:1)
另一种方法是解析log().all()
as seen in my article的输出。
注意:由于它是Apache 2.0许可的,因此与Stack Overflow's default licensing不同,这里我没有共享代码。
答案 1 :(得分:1)
基于@vamsi's answer,这是对我们有用的内容的完整实现。
POM
<http.client.version>4.5.3</http.client.version>
<curl.logger.version>1.0.5</curl.logger.version>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${http.client.version}</version>
</dependency>
<dependency>
<groupId>com.github.dzieciou.testing</groupId>
<artifactId>curl-logger</artifactId>
<version>${curl.logger.version}</version>
</dependency>
和
public class Requests {
public String baseUrl;
public RequestSpecification getRequestSpecification(String authorizationToken) {
/** Enables printing request as curl under the terminal as per https://github.com/dzieciou/curl-logger */
Options options = Options.builder()
.printMultiliner()
.updateCurl(curl -> curl
.removeHeader("Host")
.removeHeader("User-Agent")
.removeHeader("Connection"))
.build();
RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig(options);
baseUrl = Constants.getEndpoint();
RestAssured.baseURI = baseUrl;
RequestSpecification rq = given()
.config(config)
.contentType(ContentType.JSON)
.contentType("application/json\r\n")
.header("Accept", "application/json").and()
.header("Content-Type", "application/json")
.header("Authorization", authorizationToken)
.when()
.log()
.everything();
return rq;
}
}
在运行终端上生成此位:
如果在触发测试后无法在运行终端上立即看到它,或者需要将其记录到标准系统输出或.log文件中,请在测试/资源下创建一个logback.xml文件如下所示的文件夹可能会对您有所帮助。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>mylog.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
</encoder>
</appender>
<logger name="curl" level="DEBUG">
<appender-ref ref="FILE"/>
</logger>
PS:在系统输出或.log文件之间进行切换是基于您传递的哪个作为“ curl”记录器附加程序的参考的。
答案 2 :(得分:0)
包括以下依赖项。
行家:
</dependency>
<dependency>
<groupId>com.github.dzieciou.testing</groupId>
<artifactId>curl-logger</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
</dependency>
配置完成后,使用以下代码在控制台输出中记录curl请求。
package com.api.test;
import static org.hamcrest.CoreMatchers.is;
import org.json.JSONObject;
import org.testng.annotations.Test;
import com.sample.model.EnrollEmail;
import com.github.dzieciou.testing.curl.CurlLoggingRestAssuredConfigFactory;
import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
/**
* @author vamsiravi
*
*/
public class Sample {
@Test(enabled=true)
public void sampleAPITest() {
RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig();
EnrollEmail email = new EnrollEmail();
email.setEmailAddress("sample@domain.com");
RestAssured.given().config(config).contentType(ContentType.JSON).body(email).when().post("https://jsonplaceholder.typicode.com/posts").then()
.assertThat()
.statusCode(201);
}
}
输出:
[main]调试curl-卷曲'https://jsonplaceholder.typicode.com/posts'-H'接受: / '-H'内容类型:application / json; charset = UTF-8'-H'主机:jsonplaceholder.typicode.com'-H'连接:Keep-Alive'-H'用户代理:Apache-HttpClient / 4.5.3(Java / 1.8.0_181)'-数据二进制文件'{“ emailAddress”:“ sample@domain.com”}'--compressed -k -v
通过:sampleAPITest