Verify that the API response contains the correct resource representation when the resource contains nested objects or arrays.
Language: Java
Framework: Rest assured
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import org.json.JSONObject;4import org.testng.Assert;5import org.testng.annotations.Test;67public class APITest {89 @Test10 public void testNestedObjectsAndArrays() {11 12 RestAssured.baseURI = "http://example.com";13 14 // set expected response json15 JSONObject expectedResponseJson = new JSONObject()16 .put("id", 123)17 .put("name", "John Doe");18 19 // set nested object in expected response json20 JSONObject nestedJson = new JSONObject()21 .put("id", 456)22 .put("name", "Jane Doe");23 expectedResponseJson.put("nestedObject", nestedJson);24 25 // set array in expected response json26 expectedResponseJson.put("array", new String[]{"element1", "element2"});27 28 // send GET request to API endpoint29 Response response = RestAssured.given().get("/api/endpoint");3031 // assert response contains expected nested objects and arrays32 Assert.assertEquals(response.getStatusCode(), 200, "Response is not successful");33 JSONObject actualResponseJson = new JSONObject(response.getBody().asString());34 35 Assert.assertEquals(actualResponseJson, expectedResponseJson, "Nested objects and arrays are not as expected");36 37 // Uncomment below code to connect to remote client with desired capabilities38 /*39 DesiredCapabilities caps = new DesiredCapabilities();40 caps.setCapability("platform", "Windows 10");41 caps.setCapability("browser", "chrome");42 caps.setCapability("version", "latest");43 caps.setCapability("name", "API Nested Objects/Arrays Test");4445 RemoteWebDriver driver = new RemoteWebDriver(new URL("https://example.com/wd/hub"), caps);4647 // add rest of the code to test on remote client48 */4950 }51}
Language: Javascript
1// Mocha with Chai Assertion Library and Supertest.23//Assumptions: 4//1. API endpoint URL is http://example.com/api/resource5//2. Resource contains nested objects and arrays as specified in API documentation67const request = require('supertest');8const expect = require('chai').expect;910describe('API Testing', function() {11 it('Check nested objects/arrays in API response', function(done) {12 request('http://example.com')13 .get('/api/resource')14 .expect(200)15 .end(function(err, res) {16 if (err) done(err);17 expect(res.body).to.have.property('object1');18 expect(res.body.object1).to.have.property('property1');19 expect(res.body).to.have.property('array1');20 expect(res.body.array1).to.be.an('array');21 done();22 });23 });24});
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