package com.dw.cases;
import io.restassured.http.ContentType;
import io.restassured.http.Cookie;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class Testassured {
/**
* get带一个参数 http://jsonplaceholder.typicode.com/posts?userId=2
*/
@Test
public void testparam(){
given().
param("userId",2).
when().
get("http://jsonplaceholder.typicode.com/posts").
then().
statusCode(200).log().all();
}
/**
* get带多个参数 http://jsonplaceholder.typicode.com/posts?userId=2&id=14
*/
@Test
public void testparams(){
Map<String,String> parameters = new HashMap<String, String>();
parameters.put("userId","2");
parameters.put("id","14");
given().
params(parameters).
when().
get("http://jsonplaceholder.typicode.com/posts").
then().
statusCode(200).log().all();
}
/**
* 测试 带一个请求头字段
*/
@Test
public void testRequestWithHeader() {
given().
header("accept-encoding", "gzip,deflate").
when().log().all().
get("http://jsonplaceholder.typicode.com/posts").
then().
statusCode(200);
}
/**
* 测试 带多个头字段的请求
*/
@Test
public void testRequestWithHeaders() {
//构造一个Map对象,用来存多个参数和值
Map<String, String> headers = new HashMap<String,String>();
headers.put("accept-encoding", "gzip,deflate");
headers.put("accept-language", "zh-CN");
given().
headers(headers).
when().log().all().
get("http://jsonplaceholder.typicode.com/posts").
then().
statusCode(200);
}
/**
* 测试响应内容是xml数据
*/
@Test
public void testSingleXMLContent() {
given().
get("http://www.thomas-bayer.com/sqlrest/INVOICE/").
then().
log().all().
contentType(ContentType.XML);
}
/**
* 多个测试点一行代码去校验
*/
@Test
public void testStatusCode(){
given().
get("http://jsonplaceholder.typicode.com/posts/3").
then().
body("id", equalTo(3)).and().body("title", containsString("exercitationem repellat")); // 部分匹配用containsString,完全匹配用hasItem
}
/**
* find value by xpath
*/
@Test
public void findValueByXpath() {
given().
get("http://www.thomas-bayer.com/sqlrest/CUSTOMER/10/").
then().
body(hasXPath("/CUSTOMER/FIRSTNAME"), containsString("Sue"));
}
/**
* 一个post请求举类
*/
@Test
public void testAPostMethod() {
given().
param("pwd", "sanfeng").
param("type", "username").
param("accounts", "sanfeng").
// header("Content-Type", "application/x-www-form-urlencoded").
when().
post("http://shop-xo.hctestedu.com/index.php?s=/api/user/login&application=app").
then().statusCode(200).log().all();
}
/**
* 使用path方法提取内容
*/
@Test
public void testExtractDetailsUsingPath() {
String href =
when().
get("http://jsonplaceholder.typicode.com/photos/1").
then().
contentType(ContentType.JSON).
body("albumId", equalTo(1)).
extract().
path("url");
System.out.println(href);
when().get(href).then().statusCode(304);
}
/**
* 先拿到响应对象,然后再解析
*/
@Test
public void testFirstGetResponseThenDoActions() {
Response resp = get("http://jsonplaceholder.typicode.com/photos/1").
then().
extract().
response();
// 解析响应
System.out.println("Context Type: " + resp.contentType());
System.out.println("Status Code: " + resp.statusCode());
System.out.println("Href: " + resp.path("url"));
}
/**
* 验证响应文件类型是html
*/
@Test
public void testResponseContentType2() {
given().
get("https://www.baidu.com").
then().
statusCode(200).
contentType(ContentType.HTML);
}
/**
* 得到响应头
*/
@Test
public void testGetResponseHeaders() {
Response res = get("http://shop-xo.hctestedu.com/index.php");
// 得到一个响应header中字段
String headerCFRAY = res.getHeader("Connection");
System.out.println("Connection: " + headerCFRAY);
// 得到全部的headers
Headers headers = res.getHeaders();
for (Header h : headers) {
System.out.println(h.getName() + ":" + h.getValue());
}
}
/**
* 得到cookies
*/
@Test
public void getCookies() {
Response res = get("http://shop-xo.hctestedu.com/index.php");
Map<String, String> cookies = res.getCookies();
//遍历集合,拿到每一个cookies
for (Map.Entry<String, String> entry : cookies.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
/**
* 得到cookies详细信息
*/
@Test
public void getCookiesDetailsInfo() {
Response res = get("http://shop-xo.hctestedu.com/index.php");
//得到一个详细cookies对象
Cookie c = res.getDetailedCookie("PHPSESSID");
System.out.println("判断这个cookies是否有过期时间设定: " + c.hasExpiryDate());
System.out.println("打印cookies的过期时间: " + c.getExpiryDate());
System.out.println("判断是否值: " + c.hasValue());
}
/**
* get请求参数数据设置:queryParam
*/
@Test
public void testQueryParam() {
given().
queryParam("userId", "3").
when().
get("http://jsonplaceholder.typicode.com/posts/").
then().
statusCode(200);
}
/**
* POST请求参数设置:formParam()
*/
@Test
public void testFormParam() {
given().
formParam("name", "Tom1").
formParam("job", "Tester").
when().
get("https://reqres.in/api/users").
then().
statusCode(200).log().all();
}
@Test
public void testPataParametersType1() {
given().
pathParam("section","posts"). // 我理解这里pathparam就是给url中占位
pathParam("id","3").
when().
get("http://jsonplaceholder.typicode.com/{section}/{id}").
then().
statusCode(200);
}
/**
* 状态码断言
*/
@Test
public void testStatusInResponse() {
given().get("http://jsonplaceholder.typicode.com/photos/").then().assertThat().statusCode(200).log().all();
given().get("http://jsonplaceholder.typicode.com/photos/").then().assertThat().statusLine("HTTP/1.1 200 OK");
given().get("http://jsonplaceholder.typicode.com/photos/").then().assertThat().statusLine(containsString("OK"));
}
/**
* 响应header断言
*/
@Test
public void testResponseHeader() {
given().
get("http://shop-xo.hctestedu.com/index.php").
then().
assertThat().header("Cache-Control","no-store, no-cache, must-revalidate");
given().
get("http://shop-xo.hctestedu.com/index.php").
then().
assertThat().headers("Server","nginx","Vary", containsString("Accept"));
}
/**
* 响应时间
* 这个包含,http请求加响应处理时间 加上我们使用rest assured这个工具产生的时间之和
*/
@Test
public void testResponseTime() {
long t = given().get("http://shop-xo.hctestedu.com/index.php").time();
System.out.println(t);
}
@Test
public void testResponseLessThanTime() {
given().get("http://shop-xo.hctestedu.com/index.php").then().time(lessThan(2000L));
}
/**
* 响应正文中的属性使用lambda表达式来断言
*/
@Test
public void testBodyParameterInResponse() {
given().
get("http://jsonplaceholder.typicode.com/photos/1/").
then().
body("thumbnailUrl", response -> equalTo("https://via.placeholder.com/150/92c952"));
given().
get("http://jsonplaceholder.typicode.com/photos/1/").
then().
body("thumbnailUrl", endsWith("92c952"));
}
public void testshopParam() {
given().
formParam("pwd", "sanfeng").
formParam("type", "username").
formParam("accounts", "sanfeng").
when().
get("http://shop-xo.hctestedu.com/index.php?s=/api/user/login&application=app").
then().
statusCode(200).log().all();
}
}
网友评论