QA Interview Handbook
  • ๐Ÿ Home Page
  • About Testing
    • ๐Ÿ’–Qualities of A Great Tester
  • Manual Testing
    • ๐Ÿ’กOverview
      • โœ‹Demand for Software Testing
      • ๐Ÿ˜„Tester's Role in Manual Testing
      • 7๏ธTesting Principles
      • ๐ŸšจV & V
      • โ”Interview Questions
    • โ™ป๏ธSDLC
      • ๐Ÿ“’Phase 1: Planning
      • ๐Ÿ”Phase 2: Requirement Analysis
      • ๐Ÿ‘”Phase 3: Design
      • โ›‘๏ธPhase 4: Development
      • ๐ŸงชPhase 5: Testing
      • ๐ŸššPhase 6: Deployment
      • ๐Ÿ–ฑ๏ธPhase 7: Maintenance
      • โš”๏ธCommon Challenges
      • โ”Interview Questions
    • ๐ŸŒ€STLC
    • ๐ŸŒŠWaterfall
    • โœณ๏ธAgile
      • ๐Ÿ˜Tester's Role in Scrum
    • ๐Ÿ”ขTypes
      • โฌœWhite Box Testing
      • โฌ›Black Box Testing
        • ๐Ÿ”ฐTechniques Used in Black Box Testing
        • ๐Ÿš˜Functional Testing
          • 1๏ธโƒฃUnit Testing
          • 2๏ธโƒฃIntegration Testing
            • ๐Ÿ”ฐTechniques Used in Integration Testing
          • 3๏ธโƒฃSystem Testing
            • ๐Ÿ“ผTypes of System Testing
            • ๐ŸŒŠPhases of System Testing
            • ๐ŸŒ€Regression Testing
            • ๐ŸŒซ๏ธSmoke Testing
          • 4๏ธโƒฃAcceptance Testing
            • โš™๏ธUser Acceptance Testing
            • ๐Ÿ…ฐ๏ธAlpha Testing
            • ๐Ÿ…ฑ๏ธBeta Testing
        • ๐Ÿ•ณ๏ธNon Functional Testing
      • ๐Ÿ“‘Grey Box Testing
    • ๐Ÿช„User Story
      • โบ๏ธSample User Stories
    • ๐Ÿ““Test Cases
      • โบ๏ธSample Test Cases
      • โ”Interview Questions
    • โœ–๏ธDefect Life Cycle
      • โ˜ฃ๏ธPriority + Severity
      • โบ๏ธSample Defect Reports
      • โ”Interview Questions
      • ๐Ÿ›Buggy Questions
    • ๐ŸŒAtlassian JIRA
      • ๐ŸžJIRA Issues
      • โ”Interview Questions
    • โ”Interview Questions
  • Accessibility Testing
    • ๐Ÿ’กOverview
    • ๐Ÿค“Tester's Role in Accessibility Testing
    • ๐Ÿ“šWCAG Principles
      • ๐Ÿ‘๏ธPerceivable
      • ๐ŸนOperable
      • ๐Ÿง Understandable
      • ๐Ÿค–Robust
    • ๐Ÿ”งAxe DevTools
      • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • โ”Interview Questions
  • API Testing
    • ๐Ÿ’กOverview
    • ๐Ÿ˜€Tester's Role in API Testing
    • ๐ŸŠHTTP Methods & CRUD
      • ๐Ÿ‚HTTP Status Codes
    • ๐ŸAPI Tools
      • ๐ŸŸ Postman
        • โ˜„๏ธSending your first API request
        • ๐Ÿ”ฌHTTP Requests with Java
        • ๐ŸŽฒGitHub Sample
        • โ”Interview Questions
      • โ›‘๏ธREST Assured
        • ๐ŸŽ‡Dependency
        • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • ๐ŸฆงAPI Cheatsheet
    • โ”Interview Questions
  • Database Testing
    • ๐Ÿ’กOverview
    • ๐Ÿ˜†Tester's Role in Database Testing
    • ๐Ÿ”ตSQL
      • โ›“๏ธConstraints
      • ๐Ÿ›ข๏ธReferencing a Column
      • ๐Ÿ”ผDDL Commands
      • ๐Ÿ”ผDML Commands
        • ๐Ÿ–Œ๏ธOperators
        • ๐Ÿ› ๏ธFunctions
          • โฏ๏ธAggregate Functions
        • ๐ŸŽ…Clauses
          • โซJoin Clauses
          • ๐Ÿ”ตFilter Clauses
          • โฌSet Operations
      • ๐ŸƒWildcard Character
      • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • ๐ŸงคSQL Practice Sites
    • ๐ŸซSQL Cheatsheet
    • โ”Interview Questions
  • Java
    • โ›ฉ๏ธIntroduction
    • ๐Ÿ˜„Tester's Reason to Learn Java
    • โ“‚๏ธMain Method
      • โ”Interview Questions
    • ๐Ÿ“Variables & Types
      • ๐ŸชขSpecial Types
    • ๐ŸฅModifiers
    • ๐Ÿ…พ๏ธOperators
    • ๐ŸชกString
      • ๐ŸฉบString Methods
        • String Method Problems
      • ๐ŸšจDelimiter
      • โ”Interview Questions
    • ๐Ÿ–‡๏ธConditionals
      • ๐Ÿ’ŽCommon If Statements
      • ๐Ÿ’ŽCommon Ternary Operator Statements
    • โ“‚๏ธMath Class
    • ๐ŸŒŠLoops
      • ๐Ÿ’ŽCommon Loop Examples
      • ๐Ÿ”ƒNested For Loops
    • ๐ŸผOOPS
      • ๐Ÿ›๏ธClasses and Objects
        • โ”Interview Questions
      • ๐ŸŽƒConstructor
        • โšกStatic
          • โ”Interview Questions
        • ๐Ÿ“This() & Super()
          • โ”Interview Questions
        • ๐Ÿ€Finalization
      • ๐Ÿ”“Encapsulation
      • ๐ŸฅInheritance
      • ๐Ÿฆ‹Polymorphism
      • ๐Ÿ•ธ๏ธAbstraction
    • ๐ŸฎJava Practice Sites
    • โ˜‘๏ธData Structures + Algorithms
      • ๐Ÿ…พ๏ธBig O
      • โ˜‘๏ธData Structures
        • ๐Ÿ”ธArray
        • ๐Ÿ”ณArray Problems
        • Page
      • ๐ŸชŸSliding Window Technique
        • ๐ŸชŸSliding Window Problems
        • ๐ŸฅLeetCode #53
        • ๐ŸฅLeetCode #209
    • โ”Interview Questions
  • Automation Testing
    • ๐ŸšฐFlow
      • ๐Ÿ’กOverview
      • ๐ŸคฉTester's Role in Automation Testing
      • ๐Ÿ€Selenium
        • ๐Ÿ•ธ๏ธSelenium WebDriver
          • ๐Ÿ•ท๏ธWebDriver Commands
            • ๐ŸŒWebElement
              • ๐Ÿ”†HTML Tags
              • ๐Ÿ”ฌFind Element(s)
              • ๐ŸฆŽLocators
                • โŒXpath
                • ๐ŸฐCSS Selector
                • ๐Ÿ“€DOM
                • ๐Ÿ Quick Reference for XPath + CSS
            • โœ‹Waits
            • Browser Management
            • ๐ŸŽ๏ธNavigation
            • Alerts
          • ๐Ÿท๏ธAdvanced User Interactions
            • ๐Ÿ—ฏ๏ธAction vs. Actions
            • ๐Ÿ’งDrop Down
            • โœ…Check Box
            • ๐Ÿ–‡๏ธForms
          • โš ๏ธExceptions
        • ๐ŸOOPS + Selenium
        • ๐ŸšขFrameworks
          • โš“Module Based Framework
          • ๐ŸŽนKeyword Driven Framework
          • ๐ŸŽ‹Data Driven Framework
          • ๐ŸŒบHybrid Framework
          • ๐ŸŒดLog4j
          • ๐Ÿ“„Page Object Model
        • ๐ŸงชTesting Frameworks
          • ๐Ÿ’กTestNG
          • ๐Ÿ‰‘JUnit
          • ๐Ÿฅ’BDD
            • ๐Ÿฅ’Cucumber
        • ๐ŸŒ‰Selenium Grid
          • โœ–๏ธDesired Capabilities
        • โ”Interview Questions
      • ๐Ÿ”„API Testing with Selenium
      • โชDatabase Testing with Selenium
      • โ“‚๏ธMaven
      • ๐Ÿ™Git
        • โ”Interview Questions
      • ๐Ÿ•ต๏ธโ€โ™‚๏ธJenkins
        • โ”Interview Questions
      • ๐ŸณDocker
        • โ”Interview Questions
      • ๐Ÿ“™AWS
        • โ”Interview Questions
  • Behavioral
    • ๐Ÿ“ฃMixed Interview Questions
    • โญSTAR Method
      • ๐ŸŒŸSample Responses
Powered by GitBook
On this page
  • API Testing with Selenium
  • GetTestWithPathVariable
  • GetTestWithQueryParameter
  • SimpleDeleteTest
  • SimpleGetTest
  • SimplePatchTest
  • SimplePostTest
  • SimplePutTest

Was this helpful?

  1. Automation Testing
  2. Flow

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");
    }
}
PreviousInterview QuestionsNextDatabase Testing with Selenium

Last updated 1 year ago

Was this helpful?

๐Ÿšฐ
๐Ÿ”„