API Testing : Check identifier-based retrieval

Verify that the API returns the correct resource based on the provided resource identifier.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint as https:/​/​example.com/​api and resource identifier as "1234"2/​/​Using Rest Assured framework for API testing34import io.restassured.RestAssured;5import io.restassured.response.Response;6import io.restassured.specification.RequestSpecification;78public class APITest {910 public static void main(String[] args) {1112 /​/​Creating a RequestSpecification object13 RequestSpecification httpRequest = RestAssured.given();14 15 /​/​Assuming the resource identifier "1234" is passed in the API endpoint16 String endpoint = "https:/​/​example.com/​api/​1234";17 18 /​/​Specifying the base URL of the API19 RestAssured.baseURI = endpoint;2021 /​/​Sending the GET request to the API endpoint22 Response response = httpRequest.get(endpoint);2324 /​/​Fetching the API response body in String format25 String responseBody = response.getBody().asString();26 27 /​/​Printing the API response body28 System.out.println("Response Body is => " + responseBody);2930 /​/​Asserting the API response to verify if the correct resource is returned based on the provided resource identifier31 String expectedResource = "resource_1234";32 Assert.assertEquals(responseBody, expectedResource);33 34 /​/​Uncomment below lines to connect to remote client with desired capabilities35 /​*DesiredCapabilities capabilities = new DesiredCapabilities();36 capabilities.setCapability("platformName", "Android");37 capabilities.setCapability("deviceName", "emulator-5554");38 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4723/​wd/​hub"), capabilities);*/​39 }40}

Language: Javascript

copy
1/​/​ Mocha and Chai with Axios23const axios = require('axios');4const chai = require('chai');5const expect = chai.expect;67describe('API Testing', () => {8 it('should return the correct resource based on the provided resource identifier', async () => {9 const resourceId = '123';10 const response = await axios.get(`https:/​/​example.com/​api/​resources/​${resourceId}`);11 12 /​/​ Assuming that the response will be in JSON format with a 'data' property containing the resource data13 const resource = response.data;14 15 /​/​ Assuming that the resource will have an 'id' property. Checking if the returned resource has the same ID as requested16 expect(resource.id).to.equal(resourceId);17 });18});1920/​/​ Code to run the test on a Selenium Grid with desired capabilities21/​/​ var webdriver = require('selenium-webdriver');22/​/​ var capabilities = webdriver.Capabilities.chrome();23/​/​ capabilities.set('platform', 'WINDOWS');24/​/​ capabilities.set('version', '10');25/​/​ var driver = new webdriver.Builder()26/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')27/​/​ .withCapabilities(capabilities)28/​/​ .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.

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