API Testing : Check partial string search

Verify that the API returns the correct results when searching for a partial string.

Language: Java

Framework: Rest assured

copy
1import io.restassured.RestAssured;2import io.restassured.http.ContentType;3import io.restassured.response.Response;45public class PartialStringSearchTest {6 7 /​/​Assuming base URL as "https:/​/​example.com/​api/​"8 private static final String BASE_URL = "https:/​/​example.com/​api/​";9 10 /​/​Assuming endpoint for partial string search as "search?q="11 private static final String PARTIAL_SEARCH_ENDPOINT = "search?q=";12 13 /​/​Assuming partial string to be searched as "example"14 private static final String PARTIAL_STRING = "example";15 16 public static void main(String args[]) {17 18 RestAssured.baseURI = BASE_URL;19 20 /​/​Sending GET request to search for partial string21 Response response = RestAssured.given().contentType(ContentType.JSON)22 .get(PARTIAL_SEARCH_ENDPOINT + PARTIAL_STRING);23 24 /​/​Checking if response status code is 20025 if(response.getStatusCode()==200) {26 System.out.println("Partial string search is successful");27 }28 else {29 System.out.println("Partial string search is unsuccessful");30 }31 32 /​/​Code to connect to remote client with desired capabilities33 /​*34 DesiredCapabilities caps = DesiredCapabilities.chrome();35 caps.setPlatform(Platform.WINDOWS);36 caps.setCapability("version", "87.0");37 caps.setCapability("name", "Partial String Search Test");38 39 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);40 driver.get(BASE_URL + PARTIAL_SEARCH_ENDPOINT + PARTIAL_STRING);41 */​42 }43}

Language: Javascript

copy
1/​/​ Mocha and Chai.234/​/​Assumptions: 5/​/​1. An API endpoint is already implemented to handle the search functionality.6/​/​2. The API call takes in the search term as a query parameter.7/​/​3. The API endpoint returns a JSON response with the search results.8/​/​4. The API endpoint is running on a local server.9/​/​10/​/​To connect to a remote client, uncomment the lines that set the desired capabilities and the remote server URL.1112const assert = require('chai').assert;13const axios = require('axios');14/​/​const {Builder} = require('selenium-webdriver');15/​/​const {Options} = require('selenium-webdriver/​chrome');1617const endpointUrl = 'http:/​/​localhost:5000/​api/​search'; /​/​replace with actual endpoint URL18/​/​const remoteUrl = 'http:/​/​localhost:4444/​wd/​hub'; /​/​replace with actual remote server URL19/​/​const capabilities = Options.chrome(); /​/​replace with desired capabilities2021describe('Partial string search', function() {22 it('Returns correct results', async function() {23 const searchTerm = 'book'; /​/​replace with actual partial string24 25 /​/​const driver = await new Builder().withCapabilities(capabilities).usingServer(remoteUrl).build(); /​/​connect to remote client26 /​/​const response = await driver.get(endpointUrl + `?q=${searchTerm}`); /​/​make API request27 const response = await axios.get(endpointUrl, {params: {q: searchTerm}}); /​/​make API request28 29 const results = response.data;30 assert.isArray(results, 'Search results should be an array');31 32 for (const result of results) {33 const name = result.name.toLowerCase();34 assert.include(name, searchTerm, 'Search results should include partial search term');35 } 36 });37});

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