API Testing : Check response time

Verify that the API response time is within acceptable limits.

Language: Java

Framework: Rest assured

copy
1import static io.restassured.RestAssured.given;23public class APITest {45 public static void main(String[] args) {6 /​/​Assuming the API endpoint is http:/​/​example.com/​api and the acceptable response time is 5000ms7 given().8 when().9 get("http:/​/​example.com/​api").10 then().11 assertThat().12 time(lessThan(5000L));13 14 /​/​ Code to connect to remote client with desired capabilities - if running on remote client.15 /​/​ Assuming the remote client's URL is http:/​/​192.168.0.1:4444/​wd/​hub and desired capabilities are set.16 17 /​/​ import org.openqa.selenium.remote.DesiredCapabilities;18 /​/​ import org.openqa.selenium.remote.RemoteWebDriver;19 /​/​ import java.net.URL;20 21 /​/​ DesiredCapabilities capabilities = DesiredCapabilities.chrome();22 /​/​ RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​192.168.0.1:4444/​wd/​hub"), capabilities);23 }24}

Language: Javascript

copy
1/​/​ Mocha and Chai. 23/​/​ Assumptions: 4/​/​ - The API endpoint is publicly available. 5/​/​ - The acceptable response time limit is 200ms. 67const { expect } = require('chai');8const axios = require('axios');9const TIMEOUT = 200; 1011describe('API Response Time Test', function() {12 /​/​ This test case is to verify that the API response time is within allowed time limits. 13 it('should have a response time of less than ' + TIMEOUT + 'ms', async function() {14 /​/​ Set the API endpoint URL. 15 const apiUrl = 'https:/​/​api.example.com/​v1/​test';1617 try {18 /​/​ Start the timer before making the API request. 19 const startTime = Date.now();2021 /​/​ Make the API request using axios. 22 await axios.get(apiUrl);2324 /​/​ Calculate the response time by subtracting the start time from the current time. 25 const responseTime = Date.now() - startTime;2627 /​/​ Verify that the response time is less than the acceptable time limit. 28 expect(responseTime).to.be.lessThan(TIMEOUT);29 } catch(error) {30 /​/​ Handle any errors that occur during the API request. 31 console.error(error);32 }33 });34});3536/​/​ Uncomment the code below to use remote WebDriver options instead of local driver. 37/​*38const webdriver = require('selenium-webdriver');39const remote = require('selenium-webdriver/​remote');4041const browserName = 'chrome';42const platformName = 'Windows 10';43const versionNumber = 'latest';4445const options = new webdriver.Options();46options.setChromeOptions(new chrome.Options());47options.setFirefoxOptions(new firefox.Options());4849const capabilities = {50 browserName: browserName,51 platformName: platformName,52 browserVersion: versionNumber,53 'selenoid:options': {54 enableVNC: true,55 },56};5758const driver = new webdriver.Builder()59 .usingServer('http:/​/​localhost:4444/​wd/​hub')60 .withCapabilities(capabilities)61 .setFirefoxOptions(options.getFirefoxOptions())62 .setChromeOptions(options.getChromeOptions())63 .build();64*/​

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