API Testing : Check special character search

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

Language: Java

Framework: Rest assured

copy
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import org.testng.Assert;45public class SpecialCharacterSearchTest {67 public void verifySpecialCharacterSearch() {8 /​/​Assuming the endpoint is http:/​/​localhost:8080/​api/​search9 10 /​/​Test case data11 String searchString = "special@character?search";12 13 /​/​Building request14 Response response = RestAssured15 .given()16 .param("searchString", searchString)17 .when()18 .get("/​search");19 20 /​/​Verifying response code21 Assert.assertEquals(response.getStatusCode(), 200);22 23 /​/​Verifying response content24 String responseBody = response.getBody().asString();25 Assert.assertTrue(responseBody.contains("Expected Result"));26 27 /​/​Code to use remote client with desired capabilities28 /​*29 DesiredCapabilities caps = new DesiredCapabilities();30 caps.setCapability("browserName", "Chrome");31 caps.setCapability("platform", "Windows 10");32 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);33 */​34 }35}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ - API base URL: https:/​/​example.com/​api/​5/​/​ - Endpoint for special character search: /​search6/​/​ - Query parameter for search term: q7/​/​ - Expected response format: JSON8/​/​ - Response contains an array of matching items910const assert = require('chai').assert;11const fetch = require('node-fetch');1213describe('Special Character Search API Test', function() {1415 const baseUrl = 'https:/​/​example.com/​api/​';1617 it('should return the correct results for search term with special characters', async function() {1819 /​/​ Test data20 const searchTerm = 'special#chars';21 22 /​/​ Send API request23 const response = await fetch(`${baseUrl}/​search?q=${encodeURIComponent(searchTerm)}`);24 const data = await response.json();2526 /​/​ Assertion27 assert.isArray(data, 'API response should be an array of matching items');28 assert.isNotEmpty(data, 'API response should not be empty');29 data.forEach(item => assert.include(item.name, searchTerm, 'Matching item should contain the search term'));3031 });3233});3435/​/​ To connect to remote client, add the following commented code:3637/​/​ const { Builder } = require('selenium-webdriver');38/​/​ const { Options } = require('selenium-webdriver/​chrome');3940/​/​ /​/​ Configure desired capabilities41/​/​ const chromeOptions = new Options();42/​/​ chromeOptions.addArguments('--headless');43/​/​ chromeOptions.addArguments('--no-sandbox');44/​/​ chromeOptions.addArguments('--disable-dev-shm-usage');4546/​/​ /​/​ Connect to remote WebDriver47/​/​ const driver = await new Builder()48/​/​ .forBrowser('chrome')49/​/​ .setChromeOptions(chromeOptions)50/​/​ .usingWebDriverProxy('http:/​/​proxy.example.com:8080')51/​/​ .usingServer('http:/​/​remote.example.com:4444/​wd/​hub')52/​/​ .build();

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