API Testing : Check response fields

Verify that the API response contains all the expected fields.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that the API endpoint URL is "https:/​/​example.com/​api/​v1/​users"2/​/​Assuming that the expected response fields are "id", "name", "email", and "phone"34import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class ApiTest {8 9 @Test10 public void testApi() {11 12 /​/​Local Driver 13 given()14 .when()15 .get("https:/​/​example.com/​api/​v1/​users")16 .then()17 .statusCode(200)18 .body("id", notNullValue())19 .body("name", notNullValue())20 .body("email", notNullValue())21 .body("phone", notNullValue());2223 /​/​Connect to Remote Client24 /​*25 DesiredCapabilities capabilities = DesiredCapabilities.firefox();26 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);27 */​28 29 /​/​assuming that the expected API response fields must be non-null and non-empty30 /​/​with this assumption in mind, we can add additional assertions to check the response fields' type and value as required31 }32}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. The API endpoint is accessible5/​/​2. The expected fields in the response are known67/​/​Code:89const chai = require('chai');10const chaiHttp = require('chai-http');11const expect = chai.expect;1213chai.use(chaiHttp);1415describe('API Testing', function() {16 it('should return all expected fields in the response', function(done) {17 chai.request('http:/​/​localhost:8000')18 .get('/​api')19 .end(function(err, res) {20 expect(res).to.have.status(200);21 expect(res.body).to.be.an('object');22 expect(res.body).to.have.all.keys('field1', 'field2', 'field3');23 done();24 });25 });26});2728/​/​To connect to a remote client with desired capabilities, uncomment the following code:2930/​*31const webdriver = require('selenium-webdriver');32const capabilities = webdriver.Capabilities.chrome();33capabilities.set('chromeOptions', { 'args': ['--headless', '--disable-gpu', '--no-sandbox'] });3435const driver = new webdriver.Builder()36 .forBrowser('chrome')37 .withCapabilities(capabilities)38 .usingServer('http:/​/​remote-client:4444/​wd/​hub') /​/​Replace with actual remote client URL39 .build();40 41/​/​Your test code using the remote driver goes here4243driver.quit();44*/​

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