API Testing : Check API analytics

Verify that the API correctly handles API analytics and returns the correct resources for each API analytic metric.

Language: Java

Framework: Rest assured

copy
1/​/​Assumptions: 2/​/​1. The API endpoint for analytics is "https:/​/​api.example.com/​analytics"3/​/​2. The API returns JSON response4/​/​3. The expected metrics for API analytics are "number_of_requests", "response_time", "error_rate"56import org.junit.Test;7import io.restassured.RestAssured;8import static io.restassured.RestAssured.*;9import static org.hamcrest.Matchers.*;1011public class APITest {1213 @Test14 public void testAPIAnalytics() {1516 /​/​Connecting to remote client with desired capabilities17 /​/​RestAssured.baseURI = "http:/​/​remote.client.com";18 /​/​RestAssured.port = 8080;19 /​/​RestAssured.useRelaxedHTTPSValidation();2021 /​/​Sending GET request to API endpoint for analytics22 given()23 .when().get("https:/​/​api.example.com/​analytics")24 .then().statusCode(200)25 /​/​Verifying that the API returns JSON response26 .and().contentType("application/​json")27 /​/​Verifying that the API returns the expected metrics for API analytics28 .and().body("metrics.number_of_requests", notNullValue())29 .and().body("metrics.response_time", notNullValue())30 .and().body("metrics.error_rate", notNullValue());31 }32}

Language: Javascript

copy
1/​/​ Mocha + Chai.23/​/​Assuming the API returns a JSON object with analytics data4/​/​Assuming there are 3 specific analytic data points: visits, clicks and conversions56const expect = require('chai').expect;7const request = require('request');89describe('API Analytics Test', function() {10 it('should return the correct resources for each API analytic metric', function(done) {11 request.get('http:/​/​api.example.com/​analytics', function(error, response, body) {12 expect(response.statusCode).to.equal(200);13 let analyticsData = JSON.parse(body);14 expect(analyticsData).to.haveOwnProperty('visits');15 expect(analyticsData).to.haveOwnProperty('clicks');16 expect(analyticsData).to.haveOwnProperty('conversions');17 expect(analyticsData.visits).to.be.a('number');18 expect(analyticsData.clicks).to.be.a('number');19 expect(analyticsData.conversions).to.be.a('number');20 done();21 });22 });23});2425/​/​ Uncomment the following to run on remote server with desired capabilities.2627/​/​var webdriver = require('selenium-webdriver');28/​/​var capabilities = webdriver.Capabilities.chrome();29/​/​capabilities.set('chromeOptions', {args: ['--headless', '--disable-gpu', '--no-sandbox']});30/​/​var driver = new webdriver.Builder()31/​/​.usingServer('http:/​/​localhost:4444/​wd/​hub')32/​/​.withCapabilities(capabilities)33/​/​.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