API Testing : Verify API response

Verify that the API response status code is 200 OK.

Language: Java

Framework: Rest assured

copy
1import org.testng.Assert;2import org.testng.annotations.Test;3import io.restassured.RestAssured;4import io.restassured.response.Response;5import static io.restassured.RestAssured.given;67public class APITesting {8 9 @Test10 public void verifyAPIResponseStatusCode() {11 /​/​Assuming API endpoint as https:/​/​example.com/​api/​getdata12 RestAssured.baseURI = "https:/​/​example.com";13 Response response = given()14 .queryParam("data", "testdata") /​/​Assuming query parameters as data to test API15 .when()16 .get("/​api/​getdata")17 .then()18 .extract().response();19 20 /​/​Verify API response code21 Assert.assertEquals(response.getStatusCode(), 200, "API response code is not 200 OK");22 }23 24 /​*25 /​/​Code to connect to remote client with desired capabilities26 public void connectToRemoteClient() {27 DesiredCapabilities caps = new DesiredCapabilities();28 caps.setCapability("browserName", "chrome");29 caps.setCapability("platformName", "windows");30 caps.setCapability("version", "87");31 /​/​Assuming remote client URL as https:/​/​example.com/​wd/​hub32 driver = new RemoteWebDriver(new URL("https:/​/​example.com/​wd/​hub"), caps);33 }34 */​35}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumption: 4/​/​API endpoint URL: https:/​/​example.com/​api/​users5/​/​API method: GET67const chai = require('chai');8const expect = chai.expect;9const axios = require('axios');1011describe('API Testing', () => {12 it('should verify the API response status code is 200 OK', async () => {13 14 const response = await axios.get('https:/​/​example.com/​api/​users');15 16 expect(response.status).to.equal(200);17 })18})1920/​/​Assumption: 21/​/​Remote driver URL: http:/​/​localhost:4444/​wd/​hub22/​/​Browser: Chrome Version 90.0.4430.212232425/​/​To use remote driver with desired capabilities26/​/​ const { Builder } = require('selenium-webdriver');27/​/​ const { Options } = require('selenium-webdriver/​chrome');2829/​/​ const options = new Options().setCapability('browserName', 'chrome');30/​/​ const driver = new Builder()31/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')32/​/​ .withCapabilities(options)33/​/​ .build();3435/​/​ /​/​To use local driver36/​/​ const { Builder } = require('selenium-webdriver');37/​/​ const { Options } = require('selenium-webdriver/​firefox');3839/​/​ const options = new Options().setProfile(new FirefoxProfile('path/​to/​profile'));40/​/​ const driver = new Builder()41/​/​ .forBrowser('firefox')42/​/​ .setFirefoxOptions(options)43/​/​ .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