Verify that the API correctly handles API monitoring and returns the correct resources for each API metric.
Language: Java
Framework: Rest assured
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import io.restassured.specification.RequestSpecification;4import org.testng.Assert;56public class APITest {78 public static void main(String[] args) {910 //Assuming the API URL to be "https://example.com/api"11 String apiURL = "https://example.com/api";12 13 //Creating a request object using RequestSpecification class14 RequestSpecification request = RestAssured.given();15 16 //Sending GET request to the API URL17 Response response = request.get(apiURL);18 19 //Printing the response body20 System.out.println(response.getBody().asString());21 22 //Verifying the response status code23 int statusCode = response.getStatusCode();24 Assert.assertEquals(statusCode, 200);25 }26}
Language: Javascript
1// Mocha and Chai.23//Assumptions4//1. The API endpoint is available and accessible with valid credentials5//2. The API metrics have been pre-defined and documented67//import necessary packages8const chai = require('chai');9const chaiHttp = require('chai-http');10const expect = chai.expect;1112chai.use(chaiHttp);1314describe('API Monitoring', function() {15 it('should return correct resources for each API metric', function(done) {16 const endpoint = "http://example.com/api"; //replace with actual endpoint17 18 //connect to remote client with desired capabilities19 const capabilities = {20 browserName: 'chrome',21 'goog:chromeOptions': {22 //set options23 }24 };25 const webdriver = require('selenium-webdriver');26 const remote = require('selenium-webdriver/remote');27 const driver = new webdriver.Builder()28 .withCapabilities(capabilities)29 .usingServer('http://localhost:4444/wd/hub')30 .build();3132 //send API request33 chai.request(endpoint)34 .get('/metrics')35 .end(function(err, res) {36 expect(res).to.have.status(200);37 expect(res.body).to.be.an('object');38 39 //verify resources for each metric40 expect(res.body).to.have.property('metric1');41 expect(res.body.metric1).to.have.property('resource');42 expect(res.body.metric1.resource).to.not.be.null;43 44 expect(res.body).to.have.property('metric2');45 expect(res.body.metric2).to.have.property('resource');46 expect(res.body.metric2.resource).to.not.be.null;4748 //close connection to remote client49 driver.quit();50 done();51 });52 });53});
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