API Testing : Check response timeout

Verify that the API returns a response within a specified timeout period.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming we have the API url and timeout limit already specified23import org.junit.Test;4import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class APITest {89 @Test10 public void testResponseTimeout() {11 given()12 .when()13 .get("https:/​/​api.example.com")14 .then()15 .statusCode(200)16 .time(lessThan(5000L)); /​/​timeout limit in milliseconds17 }18 19 /​/​Code to connect to remote client with desired capabilities20 /​*21 import org.openqa.selenium.WebDriver;22 import org.openqa.selenium.remote.DesiredCapabilities;23 import org.openqa.selenium.remote.RemoteWebDriver;24 import java.net.URL;25 26 public class RemoteWebDriverTest {27 private static WebDriver driver;28 public static void main(String[] args) {29 try {30 DesiredCapabilities capabilities = DesiredCapabilities.chrome();31 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities); /​/​localhost can be replaced with host IP address32 } catch(Exception e) {33 System.out.println(e.getMessage());34 }35 driver.get("https:/​/​www.example.com");36 }37 }38 */​39}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ - The API is already deployed and accessible.5/​/​ - The timeout period is 5 seconds.67const chai = require('chai');8const chaiHttp = require('chai-http');9const expect = chai.expect;10const url = 'https:/​/​example.com/​api';11chai.use(chaiHttp);1213describe('API Test: Check Response Timeout', function() {14 it('should return a response within 5 seconds', function(done) {15 /​/​ Set timeout period16 this.timeout(5000);1718 /​/​ Use Chai HTTP library to send GET request and verify response time19 chai.request(url)20 .get('/​')21 .end(function(err, res) {22 expect(err).to.be.null;23 expect(res).to.have.status(200);24 expect(res).to.have.header('content-type', 'application/​json');25 expect(res).to.have.property('body');26 done();27 });28 });29});3031/​/​ Uncomment and modify the following code to connect to remote client with desired capabilities.32/​/​ const webdriver = require('selenium-webdriver');33/​/​ const capabilities = webdriver.Capabilities.chrome();34/​/​ capabilities.set('version', '92.0');35/​/​ capabilities.set('platformName', 'Windows 10');36/​/​ capabilities.set('seleniumVersion', '3.141.0');37/​/​ const remoteUrl = 'http:/​/​localhost:4444/​wd/​hub';38/​/​ const driver = new webdriver.Builder()39/​/​ .withCapabilities(capabilities)40/​/​ .usingServer(remoteUrl)41/​/​ .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