Verify that the API handles concurrent requests correctly.
Language: Java
Framework: Rest assured
1// Assumptions: 2// - The API endpoint is reachable at "https://api.example.com"3// - The API accepts HTTP GET requests4// - The API response is in JSON format5// - The API returns a list of objects67import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910import org.testng.annotations.Test;1112public class ConcurrentRequestsTest {1314 @Test15 public void testConcurrentRequests() {16 baseURI = "https://api.example.com";17 18 // Enable SSL when connecting to remote client19 // DesiredCapabilities capabilities = new DesiredCapabilities();20 // capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);21 // capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);22 // RemoteWebDriver driver = new RemoteWebDriver(new URL("http://remoteclient.example.com:4444/wd/hub"), capabilities);23 24 // Send concurrent requests25 given()26 .contentType("application/json")27 .when()28 .get("/api/objects")29 .then()30 // Check if response is successful31 .statusCode(200)32 // Check number of objects returned33 .body("size()", greaterThanOrEqualTo(2));34 }35}
Language: Java
Framework: Rest assured
1//Assumptions: 2//1) The API has multiple resources that can be accessed concurrently. 3//2) The API has endpoints for each resource. 45import org.testng.annotations.Test;6import io.restassured.RestAssured;7import io.restassured.response.Response;89public class ConcurrentRequestsTest {10 11 @Test12 public void testConcurrentRequests() {13 //Local driver14 RestAssured.baseURI = "http://localhost:8080"; //Change the URL as per application environment15 16 //Remote driver with desired capabilities17 //DesiredCapabilities capabilities = DesiredCapabilities.chrome();18 //capabilities.setPlatform(Platform.WINDOWS);19 //try {20 //WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);21 //} catch (MalformedURLException e) {22 //e.printStackTrace();23 //}24 25 //Sending concurrent requests26 Response response1 = RestAssured.get("/resource1");27 Response response2 = RestAssured.get("/resource2");28 Response response3 = RestAssured.get("/resource3");29 30 //Verifying response for each request31 int statusCode1 = response1.getStatusCode();32 int statusCode2 = response2.getStatusCode();33 int statusCode3 = response3.getStatusCode();34 35 //Assertion for first request36 assert(statusCode1 == 200); //Assuming a 200 status code is returned for a successful response for resource137 38 //Assertion for second request39 assert(statusCode2 == 200); //Assuming a 200 status code is returned for a successful response for resource240 41 //Assertion for third request42 assert(statusCode3 == 200); //Assuming a 200 status code is returned for a successful response for resource343 }44}
Language: Javascript
1// Mocha and Chai.23//Assumptions: 4//1. The API endpoint of the application is stable and running.5//2. The number of concurrent requests is set to 50.6//3. Mocha and Chai frameworks are installed and set up correctly.78//Import necessary libraries9const axios = require('axios');10const {expect} = require('chai');1112describe('API Testing - Check Concurrent Requests', () => {13 14 //Test case to verify that the API handles concurrent requests correctly15 it('Should handle 50 concurrent requests without any errors', async () => {16 17 //Set up promise array for concurrent requests18 const promises = [];1920 //Create and push 50 requests to the API endpoint in the promise array21 for(let i=0; i<50; i++){22 promises.push(axios.get('http://localhost:3000/api/endpoint'));23 }2425 //Wait for all concurrent requests to complete26 const responses = await Promise.all(promises);2728 //Check if all requests were successful and returned status code 20029 responses.forEach((response) => {30 expect(response.status).to.equal(200);31 });3233 });3435 //Test case to verify that the API is not affected by high traffic volumes36 it('Should handle a high traffic volume without any errors', async () => {37 38 //Set up promise array for concurrent requests39 const promises = [];40 41 //Create and push 500 requests to the API endpoint in the promise array42 for(let i=0; i<500; i++){43 promises.push(axios.get('http://localhost:3000/api/endpoint'));44 }4546 //Wait for all requests to complete47 const responses = await Promise.all(promises);4849 //Check if all requests were successful and returned status code 20050 responses.forEach((response) => {51 expect(response.status).to.equal(200);52 });5354 });5556});5758//Code to connect to remote client with desired capabilities:59//Assumptions:60//1. The remote client has the desired browser version and the webdriver is set up correctly.61//2. The desired capabilities are set to run the test in Chrome browser.6263//Import necessary libraries64const webdriver = require('selenium-webdriver');65const {Builder, Capabilities} = require('selenium-webdriver');6667//Create desired capabilities object68const capabilities = Capabilities.chrome();6970//Set up remote browser options71const options = {72 'browserName': 'chrome',73 'version': '91.0',74 'enableVNC': true,75 'enableVideo': false76};7778//Set desired capabilities options79capabilities.set('chromeOptions', options);8081//Create remote webdriver instance using builder and desired capabilities82const driver = new Builder()83 .usingServer('http://REMOTE_CLIENT_IP:4444/wd/hub')84 .withCapabilities(capabilities)85 .build();8687//Assumption: The login page of the web application has a 'username' and 'password' field.88//Code to check login on website using Selenium 4, Python89#Assumptions:90#1. The website's login page is loaded91#2. The page has two input fields: username and password92#3. The page contains a 'Log In' button9394#import necessary libraries95from selenium import webdriver96from selenium.webdriver.common.by import By97from selenium.webdriver.common.keys import Keys98from selenium.webdriver.chrome.service import Service99100#Set up Chrome webdriver service101service = Service('/path/to/chromedriver')102service.start()103104#Create Chrome webdriver instance105driver = webdriver.Chrome(service=service)106107#Navigate to the login page of the web application108driver.get('https://example.com/login')109110#Find the 'username' and 'password' input fields and enter valid credentials111username_field = driver.find_element(By.ID, 'username')112username_field.clear()113username_field.send_keys('valid_username')114115password_field = driver.find_element(By.ID, 'password')116password_field.clear()117password_field.send_keys('valid_password')118password_field.send_keys(Keys.RETURN)119120#Find and click the 'Log In' button121login_button = driver.find_element(By.XPATH, '//button[contains(text(), "Log In")]')122login_button.click()123124#Assumptions:125#1. The web app navigates to the home page after successful login126#2. The home page contains a 'Logout' button with xpath '//button[contains(text(), "Logout")]'127128#Find 'Logout' button and verify it is displayed129logout_button = driver.find_element(By.XPATH, '//button[contains(text(), "Logout")]')130assert logout_button.displayed()131132#Close the browser window133driver.quit()
Language: Javascript
1// Mocha and Chai.23//Assumptions: 4//1. The API endpoint is known and accessible. 5//2. The API can handle concurrent requests. 6//3. The response format is JSON.78const chai = require('chai');9const chaiHttp = require('chai-http');10const expect = chai.expect;11const apiUrl = 'https://api.example.com';1213chai.use(chaiHttp);1415describe('API concurrency testing', function() {16 it('should handle concurrent requests and return the correct resources', function(done) {17 const requests = [1, 2, 3];18 const responses = [];1920 requests.forEach(function(request) {21 chai.request(apiUrl)22 .get('/resource')23 .end(function(err, res) {24 responses.push(res);25 if (responses.length === requests.length) {26 responses.forEach(function(response) {27 expect(response).to.have.status(200);28 expect(response).to.be.json;29 expect(response.body).to.have.property('resourceId');30 expect(response.body).to.have.property('resourceName');31 });32 done();33 }34 });35 });36 });37});3839//Connecting to a remote client with desired capabilities40//Assumptions:41//1. The remote client's IP address is known and accessible.42//2. The remote client has the desired capabilities to run the automation script.4344const webdriver = require('selenium-webdriver');45const capabilities = {46 browserName: 'chrome',47 'goog:chromeOptions': {48 args: ['--headless']49 }50};5152const remoteUrl = 'http://10.0.0.1:4444/wd/hub'; //Replace with the remote client's IP address and port number.5354const driver = new webdriver.Builder()55 .usingServer(remoteUrl)56 .withCapabilities(capabilities)57 .build(); 5859//Use 'driver' object to automate the test case on the remote client.
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing