> For the complete documentation index, see [llms.txt](https://qatesting.gitbook.io/qa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qatesting.gitbook.io/qa/automation-testing/flow/api-testing-with-selenium.md).

# 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 <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## GetTestWithQueryParameter <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## SimpleDeleteTest <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## SimpleGetTest <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## SimplePatchTest <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## SimplePostTest <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}

## SimplePutTest <a href="#file-name-id" id="file-name-id"></a>

{% code fullWidth="true" %}

```java
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");
    }
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/automation-testing/flow/api-testing-with-selenium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
