Verify that the API response contains the correct data for each field.
Language: Java
Framework: Rest assured
1import org.junit.*;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import io.restassured.specification.RequestSpecification;56public class APITest {78 @BeforeClass9 public static void setup() {10 RestAssured.baseURI = "http://localhost:8080"; //Assuming the API is hosted on localhost11 RestAssured.basePath = "/api";12 }1314 @Test15 public void testAPICorrectness() {16 RequestSpecification request = RestAssured.given();17 Response response = request.get("/data");1819 //Assuming the response is a JSON object20 Assert.assertEquals(200, response.getStatusCode());21 Assert.assertEquals("application/json", response.contentType());2223 //Assuming the fields are "field1", "field2", "field3"24 String field1 = response.jsonPath().getString("field1");25 String field2 = response.jsonPath().getString("field2");26 String field3 = response.jsonPath().getString("field3");2728 //Assuming the expected values for field1, field2, and field3 are "value1", "value2", and "value3", respectively29 Assert.assertEquals("value1", field1);30 Assert.assertEquals("value2", field2);31 Assert.assertEquals("value3", field3);32 }3334 @AfterClass35 public static void teardown() {36 //Code to close the local driver and connection to remote client with desired capabilities goes here as comments37 }38}
Language: Javascript
1// Mocha and Chai23//Assumptions: 4//1. API is deployed and running without errors.5//2. API endpoint URL for the test is "https://example.com/api"6//3. Expected data and field names are known.7//4. Test data and expected data are to be hardcoded for this test89const fetch = require('node-fetch');10const { expect } = require('chai');1112describe('API Testing', () => {13 it('should return correct data for each field', async () => {14 15 //Replace with actual test data16 const testData = {17 fieldNameOne: "testValueOne",18 fieldNameTwo: "testValueTwo",19 fieldNameThree: "testValueThree"20 }2122 const expectedData = {23 fieldNameOne: "expectedValueOne",24 fieldNameTwo: "expectedValueTwo",25 fieldNameThree: "expectedValueThree"26 }2728 const response = await fetch('https://example.com/api');29 const json = await response.json();3031 //Check each field in the JSON response32 expect(json.fieldNameOne).to.equal(expectedData.fieldNameOne);33 expect(json.fieldNameTwo).to.equal(expectedData.fieldNameTwo);34 expect(json.fieldNameThree).to.equal(expectedData.fieldNameThree);3536 //To test a remote client, add desired capabilities as follows37 // const webdriver = require('selenium-webdriver');38 // const remote = require('selenium-webdriver/remote');3940 // const driver = new webdriver.Builder()41 // .usingServer('remote-server-url.com') //Add URL to remote Selenium server42 // .withCapabilities(webdriver.Capabilities.chrome())43 // .setRemoteOptions(new remote.FileDetector())44 // .build();4546 //Access API with remote driver47 //const response = await driver.get('https://example.com/api');48 //const json = await response.json();49 });50});
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