API Testing : Check API error logs

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

Language: Java

Framework: Rest assured

copy
1/​/​Assuming endpoints to fetch error logs are already available23import static io.restassured.RestAssured.*;4import io.restassured.response.Response;5import org.testng.Assert;67public class APITest {8 public static void main(String[] args) {9 /​/​Test for fetching error logs for invalid API call10 Response response = given().when().get("http:/​/​localhost:8080/​api/​errorLogs/​invalidCall");11 12 /​/​Verify response status is 200 OK13 Assert.assertEquals(response.getStatusCode(),200);14 15 /​/​Verify response body contains error message for invalid API call16 Assert.assertTrue(response.getBody().asString().contains("invalidCall"));17 18 /​/​Test for fetching error logs for unauthorized access19 response = given().when().get("http:/​/​localhost:8080/​api/​errorLogs/​unauthorizedAccess");20 21 /​/​Verify response status is 401 Unauthorized22 Assert.assertEquals(response.getStatusCode(),401);23 24 /​/​Verify response body contains error message for unauthorized access 25 Assert.assertTrue(response.getBody().asString().contains("Unauthorized"));26 }27 /​/​Code for connecting to remote client with desired capabilities28 /​*assuming remote address, port, and browser capabilities are specified*/​29 public static void connectToRemoteClient() {30 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​<remote address>:<port>/​wd/​hub"), DesiredCapabilities.<browser capabilities>); 31 }32}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​ Assumptions: 4/​/​ - API URL: http:/​/​example.com/​api5/​/​ - API endpoint for error logs: /​error_logs6/​/​ - Error logs have a unique ID field called 'log_id'7/​/​ - Error logs contain the following fields: 'error_message', 'error_code', 'timestamp'8/​/​ - API returns JSON response910const { expect } = require('chai');11const fetch = require('node-fetch');1213describe('API Error Log Testing', function() {14 it('should return the correct resources for each API error log', async function() {15 const apiEndpoint = 'http:/​/​example.com/​api/​error_logs';16 const response = await fetch(apiEndpoint);17 const errorLogs = await response.json();1819 /​/​ Check if response code is 20020 expect(response.status).to.equal(200);2122 /​/​ Check if error logs contain the correct fields23 expect(errorLogs[0]).to.have.all.keys('log_id', 'error_message', 'error_code', 'timestamp');2425 /​/​ Check if error logs are in the correct format26 errorLogs.forEach(function(errorLog) {27 /​/​ Check if 'log_id' is a non-empty string28 expect(errorLog.log_id).to.be.a('string').that.is.not.empty;2930 /​/​ Check if 'error_message' is a non-empty string31 expect(errorLog.error_message).to.be.a('string').that.is.not.empty;3233 /​/​ Check if 'error_code' is an integer34 expect(errorLog.error_code).to.be.a('number').that.is.an.integer;3536 /​/​ Check if 'timestamp' is a valid ISO-8601 timestamp37 expect(errorLog.timestamp).to.match(/​^(\d{4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\.\d{3}Z$/​);38 });39 });40});4142/​/​ Uncomment below code to connect to remote client with desired capabilities4344/​/​ const { Builder } = require('selenium-webdriver');45/​/​ const { Options } = require('selenium-webdriver/​firefox');4647/​/​ const options = new Options();48/​/​ options.addArguments('--headless');4950/​/​ const capabilities = {51/​/​ browserName: 'firefox',52/​/​ browserVersion: '75.0',53/​/​ platformName: 'Ubuntu',54/​/​ 'moz:firefoxOptions': {55/​/​ args: [ '--headless' ]56/​/​ }57/​/​ };5859/​/​ const driver = new Builder()60/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')61/​/​ .withCapabilities(capabilities)62/​/​ .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