API Testing : Check parameter-based search

Verify that the API returns the correct resource based on the provided search parameters.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is "http:/​/​exampleapi.com/​search"23import io.restassured.RestAssured;4import io.restassured.response.Response;5import org.junit.jupiter.api.Assertions;67public class APITest {89 @Test10 public void testParameterBasedSearch() {11 12 /​/​Assuming the search parameters are "param1=value1" and "param2=value2"13 String param1 = "value1";14 String param2 = "value2";15 16 /​/​Connecting to the API endpoint with local driver17 RestAssured.baseURI = "http:/​/​exampleapi.com";18 Response response = RestAssured.given()19 .param("param1", param1)20 .param("param2", param2)21 .when()22 .get("/​search")23 .then()24 .statusCode(200)25 .extract().response();26 27 /​/​Verifying that the response contains the correct resource based on the search parameters28 String responseBody = response.getBody().asString();29 Assertions.assertTrue(responseBody.contains("Expected resource"));30 31 /​/​Connecting to the API endpoint with remote client using desired capabilities32 DesiredCapabilities capabilities = new DesiredCapabilities();33 capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");34 capabilities.setCapability(CapabilityType.VERSION, "78.0");35 capabilities.setCapability("platform", "Windows 10");36 capabilities.setCapability("name", "Parameter based search test");37 38 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​example.com/​path/​to/​selenium/​grid"), capabilities);39 RestAssured.driver = driver;40 41 response = RestAssured.given()42 .param("param1", param1)43 .param("param2", param2)44 .when()45 .get("/​search")46 .then()47 .statusCode(200)48 .extract().response();49 50 responseBody = response.getBody().asString();51 Assertions.assertTrue(responseBody.contains("Expected resource"));52 53 /​/​Closing the remote driver54 driver.quit();55 }56}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​Assumptions: 4/​/​1. The API endpoint URL is provided already5/​/​2. The search parameters to be used in the test case are specified67const axios = require('axios');8const expect = require('chai').expect;910describe('API Testing - Check parameter-based search', () => {11 let searchParams = {12 /​/​Specify the search parameters to be used for the test case13 /​/​For example, {name: 'John Doe', age: 25}14 };1516 before(() => {17 /​/​Add code to connect to remote client with desired capabilities18 /​/​For example, using DesiredCapabilities object to specify browser and OS19 });2021 it('should return the correct resource based on provided search parameters', async () => {22 const response = await axios.get('API endpoint URL', {23 params: searchParams24 });2526 /​/​Add assertion to verify that the response returned matches the expected resource27 /​/​For example, expecting the response to contain a user object with specified name and age28 expect(response.data).to.deep.include({name: 'John Doe', age: 25});29 });3031 after(() => {32 /​/​Add code to close the connection with the remote client, if applicable33 });34});

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