API Testing : Check API event logs

Verify that the API correctly handles API event logs and returns the correct resources for each API event log.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming API URL and endpoint2String apiUrl = "http:/​/​example.com/​api";3String apiEndpoint = "/​eventlogs";45/​/​Create RequestSpecification object6RequestSpecification request = RestAssured.given();78/​/​Add parameters to request9request.param("api_key", "<insert api key>");10request.param("start_date", "2021-01-01");11request.param("end_date", "2021-01-31");1213/​/​Send GET request and store response14Response response = request.get(apiUrl + apiEndpoint);1516/​/​Verify status code of response17assertThat(response.getStatusCode(), equalTo(200));1819/​/​Verify response contains expected data20JsonPath jsonResponse = response.jsonPath();21List<String> eventLogs = jsonResponse.getList("eventLogs");22assertThat(eventLogs, notNullValue());23assertThat(eventLogs.size(), greaterThan(0)); 2425/​/​Commented code to connect to remote client with desired capabilities26/​*27DesiredCapabilities caps = new DesiredCapabilities();28caps.setCapability("browserName", "Chrome");29caps.setCapability("platform", "Windows 10");30caps.setCapability("version", "latest");3132RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​<remote client IP>:4444/​wd/​hub"), caps);33*/​

Language: Javascript

copy
1/​/​ Mocha + Chai2/​/​Assuming the API endpoint is "api.example.com"3/​/​Assuming the API returns existing event logs in JSON format containing event name, resource name and event timestamp45const expect = require('chai').expect;6const request = require('supertest')('http:/​/​api.example.com');78describe('API Event Logs Testing', () => {9 it('should return status 200 and valid event logs', (done) => {10 request.get('/​eventlogs')11 .expect(200)12 .end((err, res) => {13 if (err) return done(err);14 const eventLogs = JSON.parse(res.text);15 expect(eventLogs.length).to.be.greaterThan(0);16 eventLogs.forEach(log => {17 expect(log).to.have.property('eventName');18 expect(log).to.have.property('resourceName');19 expect(log).to.have.property('eventTimestamp');20 });21 done();22 });23 });24 /​/​To run on remote client with desired capabilities, add the following in a before function25 /​/​before((done) => {26 /​/​ driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();27 /​/​ done();28 /​/​});29 /​/​And then change the "request" line to use a base URL with the remote client IP address30 /​/​For example: request = require('supertest')('http:/​/​192.168.0.1:3000');31});3233/​/​Note: This assumes the API has an endpoint "/​eventlogs" that returns the event logs in JSON format. If the actual endpoint or response format is different, change the code accordingly.

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