API Testing : Check error recovery

Verify that the API correctly handles error recovery and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that the API is available at http:/​/​localhost:8080/​api/​2/​/​Setting up the Rest Assured framework3import io.restassured.RestAssured;4import io.restassured.http.ContentType;5import io.restassured.response.Response;6import io.restassured.specification.RequestSpecification;78public class APITest {910 /​/​Function to test error recovery of the API11 public static void testErrorRecovery () {1213 /​/​Assuming that an invalid URL is used to test error recovery14 String invalidURL = "http:/​/​localhost:8080/​api/​login";1516 /​/​Setting up the request specification17 RequestSpecification request = RestAssured.given();18 request.contentType(ContentType.JSON);1920 /​/​Assuming that an invalid request is made to the API with an incorrect verb21 Response response = request.request("DELETE", invalidURL);2223 /​/​Verifying that the API returns the correct HTTP status code24 assert response.getStatusCode() == 405; /​/​Method not allowed2526 /​/​Assuming that the API returns a JSON response with an error message27 String expectedErrorMessage = "{\n" +28 " \"error\": \"Method not allowed\"\n" +29 "}";3031 /​/​Verifying that the API returns the correct error message32 assert response.getBody().asString().equals(expectedErrorMessage);33 }3435 /​/​Main function to run the test case36 public static void main(String[] args) {37 /​/​Calling the function to test error recovery38 testErrorRecovery();39 }40}4142/​/​Code to connect to remote client with desired capabilities43/​/​Assuming the remote client is a Selenium Grid node44/​/​Setting up the Selenium Grid URL and creating a desired capabilities object4546import org.openqa.selenium.WebDriver;47import org.openqa.selenium.remote.DesiredCapabilities;48import org.openqa.selenium.remote.RemoteWebDriver;49import java.net.URL;5051public class SeleniumTest {52 public static void main(String[] args) throws Exception {5354 /​/​Assuming that the Selenium Grid hub is available at http:/​/​localhost:4444/​wd/​hub/​55 String seleniumGridUrl = "http:/​/​localhost:4444/​wd/​hub/​";5657 /​/​Setting up desired capabilities for a Chrome browser instance58 DesiredCapabilities capabilities = DesiredCapabilities.chrome();5960 /​/​Setting up the remote driver using the Selenium Grid URL and capabilities object61 WebDriver driver = new RemoteWebDriver(new URL(seleniumGridUrl), capabilities);6263 /​/​Assuming that the URL to test is http:/​/​localhost:8080/​login64 String testUrl = "http:/​/​localhost:8080/​login";6566 /​/​Navigating to the URL using the remote driver67 driver.get(testUrl);6869 /​/​Assuming that the login page has a username input field with ID 'username'70 /​/​and a password input field with ID 'password'71 /​/​Assuming that the login button has an ID 'login-submit'72 /​/​Assuming that valid username and password values are 'user123' and 'password123'73 driver.findElement(By.id("username")).sendKeys("user123");74 driver.findElement(By.id("password")).sendKeys("password123");75 driver.findElement(By.id("login-submit")).click();7677 /​/​Assuming that the page to test after login has a welcome message78 /​/​Assuming that the welcome message has an ID 'welcome-message'79 String welcomeMessage = driver.findElement(By.id("welcome-message")).getText();8081 /​/​Verifying that the welcome message is correct after login82 assert welcomeMessage.equals("Welcome, user123");8384 /​/​Closing the remote driver85 driver.quit();86 }87}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ 1. API endpoint is already available.5/​/​ 2. Error message format is standardized as per API documentation.67const request = require('supertest');8const expect = require('chai').expect;910describe('API Error Recovery Test', () => {1112 let api;1314 beforeEach(() => {15 api = request('http:/​/​localhost:3000'); /​/​ Assuming local API endpoint.16 /​/​ For remote client connection with desired capabilities, use the following code:17 /​/​ const options = {18 /​/​ desiredCapabilities: {19 /​/​ browserName: 'chrome',20 /​/​ platform: 'Windows 10'21 /​/​ },22 /​/​ port: 4444,23 /​/​ host: '127.0.0.1'24 /​/​ };25 /​/​ api = request('http:/​/​example.com').use(seleniumWebdriver(options)); /​/​ Assuming Selenium Webdriver has been installed.26 });2728 it('should return the correct HTTP status code when an error occurs', async () => {29 const response = await api.get('/​api/​resource/​12345');30 expect(response.status).to.equal(404); /​/​ Assuming HTTP 404 status code is the expected value when resource is not found.31 });3233 it('should return the error message in the correct format when an error occurs', async () => {34 const response = await api.get('/​api/​resource/​12345');35 expect(response.body).to.deep.equal({ /​/​ Assuming error message is in JSON format.36 error: {37 code: 'RESOURCE_NOT_FOUND',38 message: 'Resource with ID 12345 not found'39 }40 });41 });4243});4445/​/​ Note: Replace '/​api/​resource/​12345' with the actual API endpoint for error recovery testing.

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing