RestAssured API Framework
Rest Assured-- core features
--jdk gives httpclient, so we can call
using httpclient
wrappers libraies aviable on top of httpclient
---Rest assured
OKHttpClient
JerseyClient
-----------------------------------
Request
-Method - get,put,post,delete
-URL - baseURL + /endpoint + query params
-HEADER :
-Request Body/PayLoad
-Auth :Oauth1 , Oauth2, JWT, BearerToken,APIKEY,Basic Auth
Response
- status
-status line
- body: jsonpath,xmlpath()
-Headers
-Cookies
open suce
java dsl
MAVEN
-maven-archetype-quick
-restAssured LIB :pom.xml
TESTNG: unit test framework
** api runs in sequal mode no parral like UI
***************************
BDD Style - approach/methodology
given, when , then
NON -BDD Approach
RestAssured--is builder patterm and method chaining
given().log().all().then().get().when().assertThat().statuscode(200);
Hamcrest Library->RestAssured have Matchers library
https://documenter.getpostman.com/view/4012288/TzK2bEa8#intro
08608080288
immutable map it means it cant be changed/updated
Map.of("name","Sudheer","age","30")
Response coming in a form of JSON Object
JSONPATH- get a JSONPATH view of the response body.
fetch whole json and get the value ,store it or assert .
equalTo- in json object{}
hasItem-Json array[]
pojo- plain old java object.
1-help me create a java object for the class
2-private data vaiblables.-- public getters/setters-encapsulation
3-public constructor
serialization- convert pojo to json
ObjectMapper(Jackson libirary)--add to pom.xml
de-serialization- convert json to pojo
Http : GET
package GETAPITests;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class GetAPIQueryParmsPathParms {
@BeforeMethod
public void setup() {
RestAssured.baseURI = "https://gorest.co.in";
}
@Test
public void apiGetParm() {
given().log().all()
.header("Authorization", "Bearer cdf88f0e405447388b7a1a71061f130bccd5f70990e848d560bafadc196b7680")
.queryParam("name", "Trivedi").queryParam("status", "active").
when().log().all().get("/public/v2/users").then().log().all().assertThat().statusCode(200)
.contentType(ContentType.JSON);
}
@DataProvider
public Object[][] getapiData() {
return new Object[][] { { "Trivedi", "active", "male" },
};
}
@Test(dataProvider = "getapiData")
public void queryParamsHashmap(String name, String status, String gender) {
Map<String, String> queryParms = new HashMap<String, String>();
queryParms.put("name", name);
queryParms.put("status", status);
queryParms.put("gender", gender);
System.out.println(queryParms);
given().log().all()
.header("Authorization", "Bearer cdf88f0e405447388b7a1a71061f130bccd5f70990e848d560bafadc196b7680")
.queryParams(queryParms).when().log().all().get("/public/v2/users").then().log().all().assertThat()
.statusCode(200).contentType(ContentType.JSON);
}
@Test
public void QueryParmsWithMap() {
Map<String, String> mapQueryParams = Map.of("name", "Trivedi", "status", "active", "gender", "male");
given().log().all()
.headers("Authorization", "Bearer cdf88f0e405447388b7a1a71061f130bccd5f70990e848d560bafadc196b7680")
.queryParams(mapQueryParams).when().log().all().get("/public/v2/users").then().log().all().assertThat()
.statusCode(200).and().contentType(ContentType.JSON);
}
// how to use Path parameter in rest assured
@DataProvider
public Object[][] getUSERIDData() {
return new Object[][] { { "6940743" }, { "6940744" }
};
}
@Test(dataProvider = "getUSERIDData")
public void pathParameterAPI(String UserID) {
// postman// https://gorest.co.in/public/v2/users/:userid/posts
/// java public/v2/users/{userid}/posts
given().log().all().pathParam("userid", UserID).when().get("/public/v2/users/{userid}/posts").then().log().all()
.assertThat().statusCode(200).and().header("Connection", "keep-alive");
}
// DataProvider- suppy the data provider method -- returns 2 dimenstions object
// Array
@Test
public void aAPIHasItem() {
// postman// https://gorest.co.in/public/v2/users/:userid/posts
/// java public/v2/users/{userid}/posts
given().log().all().when().get("/public/v2/users/7395706/posts").then().log().all().assertThat().statusCode(200)
.and().header("Connection", "keep-alive")
//.body("title", equalTo("Consequatur quisquam auctus amissio conatus doloremque minus."));
.body("title", hasItem("Consequatur quisquam auctus amissio conatus doloremque minus."));
}
}
Post:
@Test
public void createUSerAptTest_post() {
int UserID = given().log().all().contentType(ContentType.JSON)
.header("Authorization","Bearer 4bca2c8e24d669e65c44c1c6788ecdef1fe48b1acfb5e0864847e0cc8510c04c")
.body(new File("./src/test/resources/jsons/create.json"))
.when().log().all().post("/public/v2/users")
.then().log().all().assertThat().extract().path("id");
System.out.println(UserID);
}
Comments
Post a Comment