1) GET HEADERS & COOKIES
package day1_SDET;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
public class HTTPRequest {
int id;
@Test(priority=1)
void getUsers()
{
given()
.when()
.get("https://reqres.in/api/users?page=2")
.then()
.statusCode(200)
.body("page",equalTo(2));
//.log().all()
}
@Test(priority=2)
void createUser()
{
HashMap data= new HashMap();//Hard coding
data.put("first_name","amit");
data.put("last_name","Kumar");
id= given()
.contentType("application/json")
.body(data)
.when()
.post("https://reqres.in/api/users")
.jsonPath().getInt("id");
//.then()
// .statusCode(201)
// .log().all();
}
@Test(priority=3,dependsOnMethods= {"createUser"})
void updateUser()
{
HashMap data =new HashMap();
data.put("first_name","Sumit");
data.put("last_name","Kumar");
given()
.contentType("application/json")
.body(data)
.when()
.put("https://reqres.in/api/users/"+id)
.then()
.statusCode(200)
.log().all();
}
@Test(priority=4)
void deleteUser()
{
given()
.when()
.delete("https://reqres.in/api/users/"+id)
.then()
.statusCode (204);
}
}
2) HTTP REQUEST
package day1_SDET;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
public class HTTPRequest {
int id;
@Test(priority=1)
void getUsers()
{
given()
.when()
.get("https://reqres.in/api/users?page=2")
.then()
.statusCode(200)
.body("page",equalTo(2));
//.log().all()
}
@Test(priority=2)
void createUser()
{
HashMap data= new HashMap();//Hard coding
data.put("first_name","amit");
data.put("last_name","Kumar");
id= given()
.contentType("application/json")
.body(data)
.when()
.post("https://reqres.in/api/users")
.jsonPath().getInt("id");
//.then()
// .statusCode(201)
// .log().all();
}
@Test(priority=3,dependsOnMethods= {"createUser"})
void updateUser()
{
HashMap data =new HashMap();
data.put("first_name","Sumit");
data.put("last_name","Kumar");
given()
.contentType("application/json")
.body(data)
.when()
.put("https://reqres.in/api/users/"+id)
.then()
.statusCode(200)
.log().all();
}
@Test(priority=4)
void deleteUser()
{
given()
.when()
.delete("https://reqres.in/api/users/"+id)
.then()
.statusCode (204);
}
}
3) GET DATA
package day2_SDET;
import org.testng.annotations.Test;
import day1_SDET.TestData;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
public class GetData {
@Test(priority=1)
void createUser()
{
TestData data= new TestData();
data.setFirst_name("Amit");
given()
.contentType("application/json")
.body(data)
.when()
.post("https://reqres.in/api/users")
.jsonPath().getInt("id");
}
}
4) CREATE EMPLOYEE AND VERIFY INCREMENT
package Exercise4;
import org.testng.Assert;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class CreateEmployeeAndVerifyIncrement {
@Test
public void testCreateEmployeeAndVerifyCount() throws InterruptedException {
// Request payload for creating an employee
String requestBody = "{name\": \"Amit\", \"salary\": \"10000\", \"age\": \"33\"}";
// Get the initial employee count
int initialEmployeeCount = getEmployeeCount();
Thread.sleep(10000);
// Send POST request to create an employee
Response response = RestAssured.given()
.contentType("application/json")
.body(requestBody)
.when()
.post("https://dummy.restapiexample.com/api/v1/create");
// Assertion for verifying employee creation
Assert.assertEquals(response.getStatusCode(), 200, "Employee was not created successfully");
// Verify response body
String responseBody = response.getBody().asString();
Assert.assertTrue(responseBody.contains("\"status\": \"success\""));
Assert.assertTrue(responseBody.contains("\"name\": \"Amit\""));
// Get the final employee count
int finalEmployeeCount = getEmployeeCount();
// Verify that the employee count increased by +1
Assert.assertEquals(finalEmployeeCount, initialEmployeeCount + 1, "Employee count was not increased by +1");
}
private int getEmployeeCount() {
// Send GET request to get the list of employees and count them
Response response = RestAssured.get("https://dummy.restapiexample.com/api/v1/employees");
String responseBody = response.getBody().asString();
// Parse the response body to get the count of employees
int employeeCount = responseBody.split("\"id\":").length - 1;
System.out.println("Employee count is"+employeeCount);
return employeeCount;
}
}
5) DELETE EMPLOYEE AND VERIFY DECREMENT
package Exercise4;
import org.testng.Assert;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class DeleteEmployeeAndVerifyDecrement {
@Test
public void testCreateEmployeeAndVerifyCount() throws InterruptedException {
// Get the initial employee count
int initialEmployeeCount = getEmployeeCount();
// Send POST request to create an employee
Response response = given()
.contentType("application/json")
.when()
.delete("https://dummy.restapiexample.com/api/v1/delete/2");
// Assertion for verifying employee creation
Assert.assertEquals(response.getStatusCode(), 200, "Employee was not deleted successfully");
// Get the final employee count
int finalEmployeeCount = getEmployeeCount();
// Verify that the employee count increased by +1
Assert.assertEquals(finalEmployeeCount, initialEmployeeCount - 1, "Employee count was not decreased by +1");
}
private int getEmployeeCount() {
// Send GET request to get the list of employees and count them
Response response = RestAssured.get("https://dummy.restapiexample.com/api/v1/employees");
String responseBody = response.getBody().asString();
// Parse the response body to get the count of employees
int employeeCount = responseBody.split("\"id\":").length - 1;
System.out.println("Employee count is"+employeeCount);
return employeeCount;
}
}
6) GET-POST-PUT
package Exercise4;
import org.testng.Assert;
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
import org.testng.annotations.Test;
import io.restassured.response.Response;
public class GetPostPut {
@Test(priority=1)
public void getListAndCount()
{
Response response =given().
baseUri("https://dummy.restapiexample.com").
when().
get("/api/v1/employees");
int SizeOfEmployee =response.jsonPath().getList("data").size();
System.out.println("Size of employee is" +SizeOfEmployee);
Assert.assertEquals(SizeOfEmployee, 24);
}
@Test(priority=2)
public void createEmployee()
{
Response response =given().
baseUri("https://dummy.restapiexample.com").
body("{\"employee_name\":\"Amit Singh\",\"employee_salary\":\"50000\",\"employee_age\":\"30\"}").
when().
post("/api/v1/create");
int status = response.statusCode();
System.out.println(+status);
Assert.assertEquals(status, 200);
}
@Test(priority=3)
public void updateEmployee()
{
Response response =given().
baseUri("https://dummy.restapiexample.com").
body("{\"employee_name\":\"Amit Singh\",\"employee_salary\":\"50000\"").
when().
put("/api/v1/update/21");
int status = response.statusCode();
System.out.println(+status);
Assert.assertEquals(status, 200);
}
}
No comments:
Post a Comment