API Testing : Check multi-word search

Verify that the API returns the correct results when searching for a string with multiple words.

Language: Java

Framework: Rest assured

copy
1/​/​ Assumptions: 2/​/​ 1. The API endpoint for search is http:/​/​example.com/​search3/​/​ 2. Query parameters are passed in the URL with the key ‘search_query’4/​/​ 3. The API returns a JSON response with the search results5/​/​ 4. The expected results are stored in a separate file ‘expected_results.json’67import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910import org.testng.annotations.Test;11import java.io.File;1213public class APITest {14 15 @Test16 public void testMultiWordSearch() {17 18 /​/​ Input search query19 String searchQuery = "sample search query";20 21 /​/​ Set up request parameters22 given().23 param("search_query", searchQuery).24 25 /​/​ Send request and check response26 when().27 get("http:/​/​example.com/​search").28 29 then().30 assertThat().31 body("results", equalTo(new File("expected_results.json")));32 }33 34 /​/​ Uncomment the following code to run the test case on a remote client 35 /​*36 @Test37 public void testMultiWordSearchRemote() {38 39 /​/​ Input search query40 String searchQuery = "sample search query";41 42 /​/​ Set up desired capabilities for remote client43 DesiredCapabilities caps = new DesiredCapabilities();44 caps.setCapability("browserName", "Chrome");45 caps.setCapability("version", "91.0");46 caps.setCapability("platform", "Windows 10");47 48 /​/​ Set up request parameters49 given().50 param("search_query", searchQuery).51 52 /​/​ Connect to remote client and send request53 when().54 remote("http:/​/​localhost:4444/​wd/​hub", caps).55 get("http:/​/​example.com/​search").56 57 then().58 assertThat().59 body("results", equalTo(new File("expected_results.json")));60 }61 */​6263}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ - We have access to the API endpoint for the search feature5/​/​ - The API endpoint accepts a GET request with a query parameter "search" that contains the string to search6/​/​ - The API returns an array of search results in JSON format78/​/​ Import dependencies9const { expect } = require('chai');10const request = require('supertest');1112/​/​ Configurations for local testing13const API_ENDPOINT = 'http:/​/​localhost:3000/​search';1415/​/​ Test suite16describe('API Search feature', function() {17 /​/​ Test case18 it('should return the correct results for multi-word search', async function() {19 /​/​ Prepare test data20 const searchString = 'multi word search';21 22 /​/​ Perform API request23 const res = await request(API_ENDPOINT)24 .get('/​')25 .query({ search: searchString });2627 /​/​ Validate response28 expect(res.status).to.equal(200);29 expect(res.body).to.be.an('array');30 expect(res.body).to.have.lengthOf.at.least(1);31 res.body.forEach(result => {32 expect(result).to.include(searchString);33 });34 });35});

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