Verify that the API returns the correct results when searching for a string with HTML tags.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class APITest {67@Test8public void testHTMLTaggedSearch() {9 String searchString = "This is <b>bold</b> text";10 given()11 .param("search", searchString)12 .when()13 .get("http://example.com/api/search")14 .then()15 .statusCode(200)16 .body("results", hasSize(2))17 .body("results[0].title", equalTo("<b>bold</b> text"))18 .body("results[0].content", containsString("<b>"))19 .body("results[0].content", containsString("</b>"))20 .body("results[1].title", equalTo("Normal text"))21 .body("results[1].content", not(containsString("<b>")))22 .body("results[1].content", not(containsString("</b>")));23}2425// Connecting to remote client with desired capabilities26/*27DesiredCapabilities desiredCapabilities = new DesiredCapabilities();28desiredCapabilities.setBrowserName("chrome");29desiredCapabilities.setPlatform(Platform.WIN10);30 31RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), desiredCapabilities);32driver.get("http://example.com");33*/34}
Language: Javascript
1// Mocha and Chai.23// Assumption: The API endpoint to be tested is "/search"4// and the search query with HTML tags is "-name '<b>Jane</b>'"56describe('HTML-Tagged Search API', function() {7 it('should return correct results for search query with HTML tags', function() {8 9 // Assumption: The API returns results in JSON format 10 // with the following structure {id: number, name: string}11 12 // Local driver13 const fetch = require('node-fetch');14 15 // Remote client with desired capabilities16 // Uncomment to run on remote client17 // const { Builder } = require('selenium-webdriver');18 // const firefox = require('selenium-webdriver/firefox');19 // const options = new firefox.Options();20 // options.addArguments('--headless');21 // const driver = await new Builder()22 // .forBrowser('firefox')23 // .withCapabilities(options)24 // .build();25 26 const searchQuery = encodeURIComponent("-name '<b>Jane</b>'");27 const apiUrl = `http://localhost:3000/search?q=${searchQuery}`;28 29 // Assumption: The API endpoint returns 200 status code for successful request30 fetch(apiUrl)31 .then(res => res.json())32 .then(json => {33 // Assumption: The API returns an array of objects34 const results = json.results;35 // Assert that the first result has the correct name property36 chai.expect(results[0].name).to.equal("Jane");37 })38 .catch(err => {39 // Assumption: The API returns error message in JSON format 40 throw new Error(`API returned an error: ${err.message}`);41 });42 });43});
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing