本文共 3170 字,大约阅读时间需要 10 分钟。
本篇来学习一下RequestSpecBuilder和ResponseSpecBuilder,这个在这个系列第二篇文章,阅读RestAssured.java源码的时候可能提到过这个类或者相关接口。
1.RequestSpecBuilder举例
场景:浏览器打开
下面是请求头截图
下面用代码来写这个过程,主要是请求发送之前,利用RequestSpecification对象来封装一些请求数据,例如参数,例如Header。
package demo;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;import io.restassured.specification.RequestSpecification;import io.restassured.builder.RequestSpecBuilder;import static io.restassured.RestAssured.*;public class RequestSpecBuilderTest { RequestSpecification requestSpc; @BeforeClass public void setup() { RequestSpecBuilder builder = new RequestSpecBuilder(); builder.addParam("userId", "2"); builder.addHeader("Accept-Encoding", "gzip, deflate"); requestSpc = builder.build(); } @Test public void test1() { given(). spec(requestSpc).log().all(). when(). get("http://jsonplaceholder.typicode.com/posts"). then(). statusCode(200).log().all(); }}
如果这样运行,这个请求地址端口默认是8080,好奇怪。
[RemoteTestNG] detected TestNG version 6.14.3Request method: GETRequest URI: http://jsonplaceholder.typicode.com:8080/posts?userId=2Proxy:Request params: userId=2Query params: Form params: Path params: Headers: Accept-Encoding=gzip, deflate Accept=*/*
所以,需要在上面代码get中,改成
才能运行通过。
2.RespondeSpecBuilder举例
上面这种请求封装过程,在响应这块,同样也有类似的类去支持这么做。
场景:我们把一些确定每次请求回来响应数据中那些不变的内容,可以一次性封装到响应期待对象中。例如每次状态码是200,某一些Header字段等。直接来看代码
package demo;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;import static io.restassured.RestAssured.*;import io.restassured.builder.ResponseSpecBuilder;import io.restassured.specification.ResponseSpecification;import static org.hamcrest.Matchers.*;public class ResponseSpecBuilderTest { ResponseSpecification responseSpc; @BeforeClass public void setup() { ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectStatusCode(200); builder.expectHeader("Content-Type", "application/json; charset=utf-8"); builder.expectHeader("Cache-Control", "public, max-age=14400"); responseSpc = builder.build(); } @Test public void test1( ) { when(). get("http://jsonplaceholder.typicode.com/posts?userId=2"). then(). spec(responseSpc). time(lessThan(3000L)); }}
这种响应期待一起封装的作用还是使用比较多。上面我写只写了一个测试用例,使用这种模式,写多个测试用例就可以看出好处多多。
3.日志打印
我们前面多次使用了在请求或者响应之后使用log().all(), 这个是打印全部信息,现在我们来细分学习下log()这个方法用法。
第一种,只打印header 或者只打印cookes 或者只打印body
/** * 日志打印1 */ @Test public void testLogOnly() { given(). get("http://jsonplaceholder.typicode.com/photos/"). then(). //log().headers(); //log().body(); log().cookies(); }
分别放开上面注释,一个一个分别测试下,打印日志和浏览器手动访问对比看看是不是一致。
第二种情况,只有发生错误情况下打印日志
/** * 只有发生错误 情况下才打印日志 */ @Test public void testLogOnlyError() { given(). get("http://jsonplaceholder.typicode.com/phot/"). then(). log().ifError(); }
我想这个方法好,特别是自动化测试报告中,并不想每个执行过程都打印日志,出错才打印日志。
第三张情况,只有满足一些特定条件才打印日志,下面模拟响应状态码等于200才打印日志。
/** * 在特定条件下才打印日志 */ @Test public void testLogUnderConditional() { given(). get("http://jsonplaceholder.typicode.com/photos/"). then(). log().ifStatusCodeIsEqualTo(200); }
关于日志打印,这里我们来一个总结,如果是在写代码过程,我们建议使用log().all(), 全部打印出来,方便我们debug。如果是写好了接口用例,我们建议写只打印需要和必要的日志就行。例如遇到错误才打印等。
转载地址:http://dnows.baihongyu.com/