🔄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
SimpleDeleteTest
SimpleGetTest
SimplePatchTest
SimplePostTest
SimplePutTest
Last updated
Was this helpful?