Rest Get API (TestNG)
package restAssuredTest;
import org.testng.Assert;
import org.testng.annotations.Test;
//import io.restassured.RestAssured; //for first test case
import static io.restassured.RestAssured.*;//for second test case
import io.restassured.response.Response;
import java.net.*;
//Below imports are for test case 2
import static io.restassured.matcher.RestAssuredMatchers.* ;
import static org.hamcrest.Matchers.* ;
public class GetRequest {
@Test
public void getTest() {
//Response response = RestAssured.get("https://reqres.in/api/users?page=2");
Response response = get("https://reqres.in/api/users?page=2"); //we can directly use above line like this after importing static io.restassured.RestAssured.*;
System.out.println("---------------------------response.asString()--------------");
System.out.println(response.asString());
System.out.println("---------------------------response.getBody().asString()--------------");
System.out.println(response.getBody().asString());
System.out.println("---------------------------response.getStatusCode()--------------");
System.out.println(response.getStatusCode());
System.out.println("---------------------------response.getStatusLine()--------------");
System.out.println(response.getStatusLine());
System.out.println("---------------------------response.getHeader(\"content-type\")--------------");
System.out.println(response.getHeader("content-type"));
System.out.println("-----------------------response.getTime()------------------");
System.out.println(response.getTime());
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, 200, "This is not right status code");
}
@Test
public void getTestAnotherWay() {
given()
//.header("Key","Value")//if you need to give header, give here like this
//such as .header("Content-Type","application/json")
//.param("Key","Value")//if you need to give parameter, give here like this
.get("https://reqres.in/api/users?page=2")
.then()
.statusCode(200)
.body("data.id[0]",equalTo(7))//[0] is the index
.body("data.id[4]",equalTo(11))
.log().all()//this will show all the response on the console log
.body("data.first_name", hasItems("Lindsay","Byron")); //check in the collection [Michael, Lindsay, Tobias, Byron, George, Rachel]
}
}
When you test the URL https://reqres.in/api/users?page=2 , you get below data:
No comments:
Post a Comment