API Testing : Check space-contained search

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

Language: Java

Framework: Rest assured

copy
1/​/​ Assumptions:2/​/​ 1. The API URL is http:/​/​example.com/​search3/​/​ 2. The search query with spaces is "test query"4/​/​ 3. The API returns an array of search results with the following fields: id, title, description56import org.junit.Test;7import io.restassured.RestAssured;8import io.restassured.response.Response;910public class ApiTest {1112 @Test13 public void testSpaceContainedSearch() {14 /​/​ Set up the search query and expected search results15 String query = "test query";16 int expectedResults = 2;1718 /​/​ Set up the API endpoint and headers19 RestAssured.baseURI = "http:/​/​example.com";20 RestAssured.basePath = "/​search";21 RestAssured.given().header("Content-Type", "application/​json");2223 /​/​ Send the search request24 Response response = RestAssured.given().queryParam("q", query).get();2526 /​/​ Verify the response status code27 assert(response.getStatusCode() == 200);2829 /​/​ Verify the number of search results30 assert(response.getBody().path("size()") == expectedResults);3132 /​/​ Verify the search results match the query33 assert(response.getBody().path("[0].title").toString().contains(query) || 34 response.getBody().path("[0].description").toString().contains(query));35 assert(response.getBody().path("[1].title").toString().contains(query) || 36 response.getBody().path("[1].description").toString().contains(query));3738 /​/​ Uncomment this block of code to connect to remote client with desired capabilities39 /​*40 DesiredCapabilities dc = new DesiredCapabilities();41 dc.setCapability("browserName", "chrome");42 dc.setCapability("version", "latest");43 dc.setCapability("platform", "WINDOWS");44 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), dc);45 */​46 }47}

Language: Javascript

copy
1/​/​ Mocha + Chai + SuperTest.23/​/​ Assumptions - 4/​/​ 1. API endpoint is already available.5/​/​ 2. Search string contains spaces.6/​/​ 3. API will return status code 200 for successful search.7/​/​ 4. Response is in JSON format.89const request = require('supertest');10const expect = require('chai').expect;1112/​/​ Local testing13const apiPath = 'http:/​/​localhost:3000/​search' /​/​ Replace with actual endpoint1415describe('API Testing - Check space-contained search', () => {16 17 it('should return correct results for space-contained search', (done) => {18 const searchString = 'Test search string with spaces';19 20 /​/​ API call to search endpoint.21 request(apiPath)22 .get('/​search?q=' + encodeURIComponent(searchString))23 .set('Accept', 'application/​json')24 .expect(200)25 .end((err, res) => {26 if (err) return done(err);27 28 /​/​ Assertions29 expect(res.body.results).to.have.lengthOf.at.least(1); /​/​ Check if search result is not empty30 expect(res.body.query).to.equal(searchString); /​/​ Check if search query is same as searchString.31 32 done();33 });34 });35 36 /​/​ Closing the Remote Webdriver Connection37 /​/​ after(() => {38 /​/​ driver.quit();39 /​/​ });40});

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