๐API Testing with Selenium
API Testing with Selenium
To test APIs with Selenium, you would need to use a third-party library or tool that can interact with APIs. One such tool is RestAssured, which is a Java-based library that allows you to write API tests in a similar way to how you would write Selenium tests. RestAssured provides a fluent API for making HTTP requests, and it can be integrated with Selenium tests to test both the front-end and back-end of a web application.
GetTestWithPathVariable
package api_test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GetTestWithPathVariable {
private static final Logger LOGGER = LogManager.getLogger(GetTestWithPathVariable.class);
@Test
public void getSingleUser() {
LOGGER.info("------API Test: Get All Users with Query Parameter");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
String id = "2";
//Request Type
Response response = httpRequest.request(Method.GET, id);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 200
Assert.assertEquals(response.getStatusCode(), 200);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
//Validate Email
String actualEmailId = jsonPath.getString("data.email");
//Validate One particular email
String expectedEmail = "janet.weaver@reqres.in";
Assert.assertEquals(actualEmailId, expectedEmail);
LOGGER.info("-----End Test: Get All Users with Query Parameter");
}
@Test
public void attempToGetUserWithInvalidId() {
LOGGER.info("------API Test: Attempt to retrieve user with invalid id");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Negative Testing with Invalid Data
String id = "23";
//Request Type
Response response = httpRequest.request(Method.GET, id);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 404
Assert.assertEquals(response.getStatusCode(), 404);
//Validate NULL Response Body
JsonPath jsonPath = response.jsonPath();
Assert.assertEquals(jsonPath.get().toString(), "{}");
LOGGER.info("-----End Test: Attempt to retrieve user with invalid id");
}
}
GetTestWithQueryParameter
package api_test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
public class GetTestWithQueryParameter {
private static final Logger LOGGER = LogManager.getLogger(GetTestWithQueryParameter.class);
@Test
public void getUserwithQueryParameter() {
LOGGER.info("------API Test: Get All Users with Query Parameter");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Request Type
Response response = httpRequest.queryParam("page", "2").request(Method.GET);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 200
Assert.assertEquals(response.getStatusCode(), 200);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
//Validate Email
List<String> listOfEmail = jsonPath.get("data.email");
//Validate One particular email
String expectedEmail = "michael.lawson@reqres.in";
boolean emailExist = listOfEmail.contains(expectedEmail);
Assert.assertTrue(emailExist, expectedEmail + "does not exist");
LOGGER.info("-----End Test: Get All Users with Query Parameter");
}
}
SimpleDeleteTest
package api_test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SimpleDeleteTest {
private static final Logger LOGGER = LogManager.getLogger(SimpleDeleteTest.class);
@Test
public void deleteSingleUser() {
LOGGER.info("------API Test: Delete Single User");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
String id = "2";
//Request Type - DELTE
Response response = httpRequest.request(Method.DELETE, id);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 204
Assert.assertEquals(response.getStatusCode(), 204);
LOGGER.info("-----End Test: Delete Single User");
}
}
SimpleGetTest
package api_test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
public class SimpleGetTest {
private static final Logger LOGGER = LogManager.getLogger(SimpleGetTest.class);
@Test
public void getAllUsers() {
LOGGER.info("------API Test: Get All Users");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Request Type
Response response = httpRequest.request(Method.GET);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 200
Assert.assertEquals(response.getStatusCode(), 200);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
//Validate Email
List<String> listOfEmail = jsonPath.get("data.email");
//Validate One particular email
String expectedEmail = "george.bluth@reqres.in";
boolean emailExist = listOfEmail.contains(expectedEmail);
Assert.assertTrue(emailExist, expectedEmail + "does not exist");
LOGGER.info("-----End Test: Get All Users");
}
}
SimplePatchTest
package api_test;
import com.github.javafaker.Faker;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SimplePatchTest {
private static final Logger LOGGER = LogManager.getLogger(SimplePatchTest.class);
@Test
public void updateUserSingleField() {
LOGGER.info("------API Test: Update User's Single Field");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Fake Data Generation
Faker faker = new Faker();
String userRole = faker.job().title();
LOGGER.debug("Updated User Job: " + userRole);
//Add Data to Raw JSON Body
JSONObject reqBody = new JSONObject();
reqBody.put("job", userRole);
//Create Header
httpRequest.header("Content-Type", "application/json");
//Add Body into HTTP Request
httpRequest.body(reqBody);
String id = "3";
//Request Type - PUT
Response response = httpRequest.request(Method.PATCH, id);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 200
Assert.assertEquals(response.getStatusCode(), 200);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
String actualName = jsonPath.getString("job");
Assert.assertEquals(actualName, userRole);
LOGGER.info("-----End Test: Update User's Single Field");
}
}
SimplePostTest
package api_test;
import com.github.javafaker.Faker;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
public class SimplePostTest {
private static final Logger LOGGER = LogManager.getLogger(SimplePostTest.class);
@Test
public void createNewUser() {
LOGGER.info("------API Test: Create New User");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Fake Data Generation
Faker faker = new Faker();
String fullName = faker.name().fullName();
LOGGER.debug("New User Full Name: " + fullName);
String userRole = faker.job().title();
LOGGER.debug("New User Job: " + userRole);
//Add Data to Raw JSON Body
JSONObject reqBody = new JSONObject();
reqBody.put("name", fullName);
reqBody.put("job", userRole);
//Create Header
httpRequest.header("Content-Type", "application/json");
//Add Body into HTTP Request
httpRequest.body(reqBody);
//Request Type - POST
Response response = httpRequest.request(Method.POST);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 201
Assert.assertEquals(response.getStatusCode(), 201);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
String actualName = jsonPath.getString("name");
Assert.assertEquals(actualName, fullName);
LOGGER.info("-----End Test: Create New User");
}
}
SimplePutTest
package api_test;
import com.github.javafaker.Faker;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SimplePutTest {
private static final Logger LOGGER = LogManager.getLogger(SimplePutTest.class);
@Test
public void updateUserFields() {
LOGGER.info("------API Test: Update User Fields");
//Get Endpoint
RestAssured.baseURI = "https://reqres.in/api/users";
//Load Endpoint
RequestSpecification httpRequest = RestAssured.given();
//Fake Data Generation
Faker faker = new Faker();
String fullName = faker.name().fullName();
LOGGER.debug("Updated User Full Name: " + fullName);
String userRole = faker.job().title();
LOGGER.debug("Updated User Job: " + userRole);
//Add Data to Raw JSON Body
JSONObject reqBody = new JSONObject();
reqBody.put("name", fullName);
reqBody.put("job", userRole);
//Create Header
httpRequest.header("Content-Type", "application/json");
//Add Body into HTTP Request
httpRequest.body(reqBody);
String id = "2";
//Request Type - PUT
Response response = httpRequest.request(Method.PUT, id);
LOGGER.debug(response.getBody().asPrettyString());
//Validate 200
Assert.assertEquals(response.getStatusCode(), 200);
//Validate Response Body
JsonPath jsonPath = response.jsonPath();
String actualName = jsonPath.getString("name");
Assert.assertEquals(actualName, fullName);
LOGGER.info("-----End Test: Update User Fields");
}
}
Last updated