API Testing : Check API testing

Verify that the API correctly handles API testing and returns the correct resources for each API test.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that the API is running on the URL - https:/​/​api.example.com2/​/​Assuming that there are 3 API tests available - test1, test2, test33/​/​Assuming that the response for each API test should contain a message "API test X successful"4/​/​Assuming that the expected status code for each API test is 20056import org.junit.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class APITest {11 12 @Test13 public void testAPI1() {14 given()15 .when()16 .get("https:/​/​api.example.com/​test1")17 .then()18 .statusCode(200)19 .body("message",equalTo("API test 1 successful"));20 }21 22 @Test23 public void testAPI2() {24 given()25 .when()26 .get("https:/​/​api.example.com/​test2")27 .then()28 .statusCode(200)29 .body("message",equalTo("API test 2 successful"));30 }31 32 @Test33 public void testAPI3() {34 given()35 .when()36 .get("https:/​/​api.example.com/​test3")37 .then()38 .statusCode(200)39 .body("message",equalTo("API test 3 successful"));40 }41 42 /​/​Uncomment below code to connect to remote client43 /​*44 @Before45 public void setUp() {46 DesiredCapabilities capabilities = new DesiredCapabilities();47 capabilities.setBrowserName("chrome");48 capabilities.setPlatform(Platform.WINDOWS);49 50 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);51 driver.manage().window().maximize();52 53 RestAssured.baseURI = "https:/​/​api.example.com";54 RestAssured.port = 80;55 }56 */​57}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​Assumptions: 4/​/​1. The API endpoint is already available and accessible.5/​/​2. The API response should contain a status code of 200.6/​/​3. The API response should contain the correct resources for each API test.78const chai = require('chai');9const chaiHttp = require('chai-http');1011chai.use(chaiHttp);1213describe('API testing', () => {14 it('Should correctly handle API testing and return the correct resources for each test', (done) => {15 chai.request('https:/​/​example.com') /​/​ replace with API endpoint URL16 .get('/​api/​testing')17 .end((err, res) => {18 expect(res).to.have.status(200); /​/​ verify status code is 20019 expect(res.body).to.have.property('resources'); /​/​ verify if resources property exists in response20 expect(res.body.resources).to.be.an('array'); /​/​ verify if resources is an array21 done();22 });23 });24});2526/​/​ Uncomment below code to connect to remote client with desired capabilities27/​/​const webdriver = require('selenium-webdriver');28/​/​const remote = require('selenium-webdriver/​remote');29/​/​const capabilities = {30/​/​ browserName: 'chrome',31/​/​ platform: 'Windows 10',32/​/​ version: 'latest'33/​/​};34/​/​35/​/​const driver = new webdriver.Builder()36/​/​ .usingServer('http:/​/​127.0.0.1:4444/​wd/​hub')37/​/​ .withCapabilities(capabilities)38/​/​ .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