Verify that the API correctly handles API accessibility and returns the correct resources for each API accessibility metric.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import static io.restassured.RestAssured.given;5import static org.hamcrest.Matchers.equalTo;67public class ApiAccessibilityTest {89@Test10public void testApiAccessibility() {1112// Base URL of the application13RestAssured.baseURI = "http://your_api_base_url.com";1415// Resource URI16String resourceURI = "/api/accessibility";1718// Request header19given().header("Content-Type", "application/json");2021// Send GET request22Response response = given().get(resourceURI);2324// Verify correct handling of API accessibility25response.then().assertThat().body("accessibilityMetric", equalTo("correct_resource"));2627}2829// Code for connecting to remote client with desired capabilities30// desiredCapabilities = DesiredCapabilities.chrome();31// remoteWebDriver = new RemoteWebDriver(new URL(remoteAddress), desiredCapabilities);32}
Language: Javascript
1// Mocha and Chai.23//Assumption: API endpoint is already deployed and accessible4//Assumption: Authentication is not needed for API access56const chai = require('chai');7const chaiHttp = require('chai-http');8const expect = chai.expect;910chai.use(chaiHttp);1112describe('API Testing - API Accessibility Test', function() {13 14 //Local driver15 let server = 'http://localhost:3000';16 17 //Remote client with desired capabilities18 // Uncomment below lines to connect to remote client19 // var webdriver = require('selenium-webdriver');20 // var capabilities = {21 // 'browserName' : 'chrome'22 // };23 // let driver = new webdriver.Builder().usingServer('http://127.0.0.1:4444/wd/hub').withCapabilities(capabilities).build();24 25 it('Should return 200 OK status code with no errors', async function() {26 27 //Assumption: API endpoint to test is /api28 const response = await chai.request(server)29 .get('/api');30 31 expect(response).to.have.status(200); //Expect 200 OK status code32 33 //Assumption: API response is in JSON format34 expect(response).to.be.json; //Expect response to be JSON35 36 //Assumption: API endpoint should have 'success' field in response37 expect(response.body).to.have.property('success');38 39 //Assumption: If success field is true then API is accessible40 expect(response.body.success).to.be.true; //Expect success field to be true41 });42});
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