使用的自动化框架
java + log4j + json + mybatis + testng + rest-assured
rest-assured相关资料:
wiki地址:https://code.google.com/p/rest-assured/wiki/Usage#Usage_Guide
GitHub地址:https://github.com/jayway/rest-assured
使用前
使用maven:
在pom.xml文件中添加:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.7.0</version>
<scope>test</scope>
</dependency>
编写脚本大致步骤
- 发起请求
- 传 header
- given().headers(header1, 值1, header2,值2, ……)
- 传 Parameters
- given().parameters("firstName", "John", "lastName", "Doe").
- 传 header
- 获取返回信息
- 打印返回的Json : log.infoJson(response.jsonPath().get());
- 打印单个字符串 :
- jsonPath.get("info.data").toString()
- jsonPaht.get("info.data[0]")
- 校验返回信息
Demo
/* *
* 登出
*/
public class LogoutTest {
LoggerControler log = LoggerControler.getLogger(LogoutTest.class);
ReturnTicketid returnTicketid = new ReturnTicketid();
String baseURL = Parameters.ACCOUNT_TEST_V1_URL;
String postPath = baseURL + "/Account/logout";
@Test
public void logout(){
String ticketid = returnTicketid.returnTicketid(Parameters.MOBILE, Parameters.PWD);
// 发起请求
Response response = given().parameters("ticket_id", ticketid).when().log().all().post(postPath);
// 打印出返回信息
log.infoJson(response.jsonPath().get());
// 校验请求成功
Assert.assertEquals(200, response.getStatusCode(), "状态200, 请求成功.");
JsonPath jsonPath = response.jsonPath();
// 校验response_status
String response_status = jsonPath.get("response_status").toString();
TestAssert.assertEquals("校验response_status", "success", response_status);
}
}