Verify that the API correctly handles API documentation and returns the correct resources for each API endpoint.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;45public class APITesting {6 7 @Test8 public void verifyAPIDocumentation() {9 10 //Assuming the API documentation URL is "http://example.com/documentation"11 RestAssured.baseURI = "http://example.com/documentation";12 13 //Sending the GET request to retrieve API documentation 14 Response response = RestAssured.get();15 16 //Verifying the response status code to be 20017 assert response.getStatusCode() == 200;18 19 //Verifying that the API documentation resources are returned for each API endpoint20 assert response.getBody().asString().contains("API endpoint 1 documentation");21 assert response.getBody().asString().contains("API endpoint 2 documentation");22 assert response.getBody().asString().contains("API endpoint 3 documentation");23 24 //If we want to connect to a remote client with desired capabilities, we can use the below code25 /*26 DesiredCapabilities caps = new DesiredCapabilities();27 caps.setCapability("browser", "Chrome");28 WebDriver driver = new RemoteWebDriver(new URL("http://example.com:4444/wd/hub"), caps);29 */30 }31}
Language: Javascript
1// Mocha + Chai + Supertest.23/**4 * Test scenario: API Testing.5 * Test case: Check API documentation.6 * Description: Verify that the API correctly handles API documentation and returns the correct resources for each API endpoint.7 * Language: Javascript.8 * Framework: Mocha + Chai + Supertest.9 *10 * Assumptions:11 * - The API documentation is available at https://example.com/docs.12 * - The API endpoints are available at https://example.com/api/v1.13 * - The API returns JSON responses.14 * - The expected response for each endpoint is defined in the documentation.15 * - The API client is running locally at http://localhost:3000.16 * - The API server is running at https://example.com.17 */1819const request = require('supertest');20const chai = require('chai');21const expect = chai.expect;2223const API_BASE_URL = 'https://example.com/api/v1';2425describe('API documentation', function () {26 it('should correctly handle API documentation and return the correct resources for each API endpoint', async function () {27 // Send GET request to API documentation28 const docResponse = await request(API_BASE_URL).get('/doc');2930 // Assert that the response status code is 200 OK31 expect(docResponse.status).to.equal(200);3233 // Assert that the response body is not empty34 expect(docResponse.body).to.not.be.empty;3536 // Parse the response body as JSON37 const docJson = JSON.parse(docResponse.text);3839 // Loop through each endpoint in the documentation40 for (const endpoint of docJson.endpoints) {41 // Send GET request to API endpoint42 const endpointUrl = API_BASE_URL + endpoint.path;43 const endpointResponse = await request(endpointUrl).get();4445 // Assert that the response status code is 200 OK46 expect(endpointResponse.status).to.equal(200);4748 // Assert that the response body is not empty49 expect(endpointResponse.body).to.not.be.empty;5051 // Assert that the response body matches the expected response in the documentation52 expect(endpointResponse.body).to.deep.equal(endpoint.response);53 }54 });55});5657// To connect to a remote client with desired capabilities:58// const webdriver = require('selenium-webdriver');59// const remote = require('selenium-webdriver/remote');60// const capabilities = webdriver.Capabilities.chrome();61// capabilities.setPageLoadStrategy('normal');62// const driver = new webdriver.Builder()63// .withCapabilities(capabilities)64// .forBrowser('chrome')65// .usingServer('http://localhost:4444/wd/hub')66// .build();
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing