API Testing : Check resource searching

Verify that the API correctly handles searching for resources based on specific criteria.

Language: Java

Framework: Rest assured

copy
1/​/​Assumptions: 2/​/​The API is up and running3/​/​There are resources available to search for4/​/​The resource search function is at endpoint "/​search"5/​/​The search criteria includes name, type and location67import org.testng.Assert;8import org.testng.annotations.Test;9import io.restassured.RestAssured;10import io.restassured.response.Response;11import io.restassured.specification.RequestSpecification;1213public class ResourceSearchTest {1415 @Test16 public void verifyResourceSearch() {17 18 /​/​Specify endpoint for resource search 19 RestAssured.baseURI = "http:/​/​localhost:8080/​api/​search";20 21 /​/​Define search criteria 22 String name = "test";23 String type = "book";24 String location = "USA";25 26 /​/​Define request specification27 RequestSpecification httpRequest = RestAssured.given();28 29 /​/​Add query parameters to the request30 httpRequest.queryParam("name", name);31 httpRequest.queryParam("type", type);32 httpRequest.queryParam("location", location);33 34 /​/​Send request and capture response35 Response response = httpRequest.get();36 37 /​/​Check response status code38 int statusCode = response.getStatusCode();39 Assert.assertEquals(statusCode, 200);40 41 /​/​Check response body for search results42 String responseBody = response.getBody().asString();43 Assert.assertTrue(responseBody.contains("search result"));44 45 /​/​Code to connect to remote client with desired capabilities46 /​*47 DesiredCapabilities capabilities = new DesiredCapabilities();48 capabilities.setBrowserName("firefox");49 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);50 */​51 }52}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming that the API endpoint is 'https:/​/​example.com/​resources' with valid authentication credentials.45/​/​Importing necessary libraries6const chai = require('chai');7const chaiHttp = require('chai-http');89/​/​Initializing chai10chai.use(chaiHttp);11chai.should();1213describe('Resource Searching API Test', () => {14 15 it('Should handle searching resource based on specific criteria', (done) => {16 /​/​Assuming resource type and keyword to search as query parameters17 const resourceType = 'book';18 const keyword = 'JavaScript';19 20 /​/​Assuming the expected response type and status code21 const responseType = 'application/​json';22 const expectedStatusCode = 200;2324 /​/​Sending a GET request to the API with query parameters25 chai.request('https:/​/​example.com')26 .get(`/​resources?type=${resourceType}&search=${keyword}`)27 .end((error, response) => {28 /​/​Checking for errors and validating response29 if (error) {30 console.log('Error occured:', error);31 done(error);32 }33 response.should.have.status(expectedStatusCode);34 response.should.be.json;35 response.body.should.have.property('resources');36 response.body.resources.should.be.a('array');37 done();38 });39 });4041 /​/​Uncomment the below code to connect to remote client with desired capabilities 42 /​*43 it('Should handle searching resource based on specific criteria on Remote client', (done) => {4445 /​/​Assuming capabilities and endpoint of remote client46 const capabilities = {47 browserName: 'chrome',48 'goog:chromeOptions': {49 w3c: true50 }51 };52 const clientEndpoint = 'https:/​/​example.selenium.com/​wd/​hub';53 54 /​/​Initializing remote browser driver55 const driver = new webdriver.Builder()56 .usingServer(clientEndpoint)57 .withCapabilities(capabilities)58 .build();59 60 /​/​Assuming same variables with updated values for the search query61 const resourceType = 'video';62 const keyword = 'Selenium';6364 /​/​Assuming the expected response type and status code65 const responseType = 'application/​json';66 const expectedStatusCode = 200;67 68 driver.get('https:/​/​example.com/​resources?type=video&search=Selenium').then(() => {69 driver.findElement(By.css('.search-box')).sendKeys(keyword);70 driver.findElement(By.css('.search-button')).click().then(() => {71 driver.wait(until.elementLocated(By.css('.resource-list')),2000);72 driver.findElement(By.css('.resource-list')).getText().then((text) => {73 console.log(`Result of remote search ${resourceType} type of ${keyword} is ${text}`);74 driver.quit();75 done();76 });77 });78 });79 });80 */​81});

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