使用的自動化框架
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>
編寫腳本大致步驟
- 發(fā)起請求
- 傳 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);
// 發(fā)起請求
Response response = given().parameters("ticket_id", ticketid).when().log().all().post(postPath);
// 打印出返回信息
log.infoJson(response.jsonPath().get());
// 校驗請求成功
Assert.assertEquals(200, response.getStatusCode(), "狀態(tài)200, 請求成功.");
JsonPath jsonPath = response.jsonPath();
// 校驗response_status
String response_status = jsonPath.get("response_status").toString();
TestAssert.assertEquals("校驗response_status", "success", response_status);
}
}