API Testing : Check API usability

Verify that the API correctly handles API usability and returns the correct resources for each API usability metric.

Language: Java

Framework: Rest assured

copy
1/​/​Assumption: This code assumes that the API endpoint URL and the associated resources for the API usability metric are known and accessible.23import org.testng.annotations.Test;4import io.restassured.response.Response;5import static io.restassured.RestAssured.*;67public class APITesting {89 @Test10 public void testAPIUsability() {11 12 /​/​Connecting to local driver13 RestAssured.baseURI = "http:/​/​localhost:8080/​";14 15 /​/​Add commented code to connect to remote client with desired capabilities16 /​*17 RemoteWebDriver driver = null;18 DesiredCapabilities dc = new DesiredCapabilities();19 dc.setCapability("platform", "WINDOWS");20 dc.setCapability("version", "10");21 try {22 driver = new RemoteWebDriver(new URL("http:/​/​10.0.2.15:4444/​wd/​hub"), dc);23 }24 catch (MalformedURLException e) {25 e.printStackTrace();26 }27 */​28 29 /​/​Sending GET request to API endpoint URL 30 Response response = given()31 .when()32 .get("/​api/​usability");33 34 /​/​Verifying that the API returns the correct resources for each API usability metric.35 response.then().assertThat().statusCode(200);36 response.then().assertThat().contentType("application/​json");37 response.then().assertThat().body("responseTime", equalTo(500));38 response.then().assertThat().body("throughput", equalTo(100));39 response.then().assertThat().body("errorRate", equalTo(0));40 }4142}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assuming the API endpoint is "https:/​/​example.com/​api/​usability"4/​/​ Assuming the API returns JSON data including "usability" field for each resource56const assert = require('chai').assert;7const request = require('request');89describe('API Testing: Usability', function () {10 it('should handle API usability correctly', function (done) {11 request.get('https:/​/​example.com/​api/​usability', function (error, response, body) {12 assert.equal(response.statusCode, 200, 'Expected response code is 200');13 assert.isObject(body, 'Expected response body to be an object');1415 /​/​ Assuming usability values are between 0 and 10016 const resources = JSON.parse(body);17 for (const resource of resources) {18 assert.isNumber(resource.usability, 'Expected usability value to be a number');19 assert.isAtLeast(resource.usability, 0, 'Expected usability value to be at least 0');20 assert.isAtMost(resource.usability, 100, 'Expected usability value to be at most 100');21 }2223 /​/​ Assuming the API endpoint supports remote clients24 /​/​ const capabilities = {25 /​/​ browserName: 'chrome',26 /​/​ platformName: 'linux',27 /​/​ browserVersion: 'latest',28 /​/​ }29 /​/​ const remoteDriver = new webdriver.Builder()30 /​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')31 /​/​ .withCapabilities(capabilities)32 /​/​ .build();33 /​/​ remoteDriver.get('https:/​/​example.com/​api/​usability', function () {34 /​/​ /​/​ perform assertions here35 /​/​ remoteDriver.quit();36 /​/​ done();37 /​/​ });38 39 done();40 });41 });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.

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