Verify that the API correctly handles sorting and filtering of resources.
Language: Java
Framework: Rest assured
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import io.restassured.specification.RequestSpecification;45public class SortingAndFilteringAPI {67 public static void main(String[] args) {8 9 // Set base uri for the API10 RestAssured.baseURI = "https://exampleapi.com";11 12 // Set request specification object13 RequestSpecification request = RestAssured.given();14 15 // Set path parameter for sorting16 request.queryParam("sort", "asc");17 18 // Set path parameter for filtering19 request.queryParam("filter", "true");20 21 // Send request and get response object22 Response response = request.get("/resources");23 24 // Verify status code is 20025 assert(response.getStatusCode() == 200);26 27 // Verify response body has correct sorting and filtering28 assert(response.getBody().asString().contains("Sorting and filtering is correct"));29 30 // Print response body31 System.out.println(response.getBody().asString());32 33 /***********************************************************************************************34 *35 * Uncomment the code below and add details to connect to remote client with desired capabilities36 * 37 ***********************************************************************************************/38 39// DesiredCapabilities capabilities = new DesiredCapabilities();40// capabilities.setCapability("browser", "chrome");41// capabilities.setCapability("browser_version", "91.0");42// capabilities.setCapability("os", "Windows");43// capabilities.setCapability("os_version", "10");44// 45// RemoteWebDriver driver = null;46// try {47// driver = new RemoteWebDriver(new URL("https://example.com/wd/hub"), capabilities);48// driver.get("https://exampleapi.com/resources");49// 50// String body = driver.findElement(By.tagName("body")).getText();51// 52// // Verify body has correct sorting and filtering53// assert(body.contains("Sorting and filtering is correct"));54// 55// // Print body56// System.out.println(body);57// } catch (MalformedURLException e) {58// e.printStackTrace();59// }60 }6162}
Language: Javascript
1// Mocha and Chai.23//Assumptions: 4//1. API endpoint is accessible 5//2. Resources exist in the API6//3. Sorting and filtering functions are implemented in the API78const chai = require('chai');9const expect = chai.expect;10const axios = require('axios');1112describe('API Testing - Sorting and Filtering', () => {1314 it('should return sorted resources', async () => {15 try {16 const response = await axios.get('https://exampleapi.com/resources?sort=asc');17 const sortedResources = response.data;1819 expect(sortedResources).to.be.an('array'); //Asserts that the response is an array2021 const firstResource = sortedResources[0];22 const lastResource = sortedResources[sortedResources.length - 1];23 24 //Asserts that the first resource comes before the last resource in ascending order25 expect(firstResource).to.be.below(lastResource).and.to.be.an('object');26 } catch (error) {27 console.log(`Error: ${error.message}`);28 }29 });3031 it('should return filtered resources', async () => {32 try {33 const response = await axios.get('https://exampleapi.com/resources?filter=example');34 const filteredResources = response.data;3536 expect(filteredResources).to.be.an('array'); //Asserts that the response is an array37 expect(filteredResources.length).to.not.equal(0); //Asserts that the response is not empty3839 //Asserts that each resource contains the string 'example' in any of its properties40 filteredResources.forEach(resource => {41 const resourceValues = Object.values(resource);42 const containsExample = resourceValues.some(value => value.includes('example'));4344 expect(containsExample).to.equal(true);45 });46 } catch (error) {47 console.log(`Error: ${error.message}`);48 }49 });5051});5253//Code to connect to remote client with desired capabilities:5455// const webdriver = require('selenium-webdriver');56// const { Builder } = require('selenium-webdriver');57// const chrome = require('selenium-webdriver/chrome');5859// const capabilities = webdriver.Capabilities.chrome();60// capabilities.set('chromeOptions', { 'args': ['--headless', '--disable-gpu' ]});6162// const driver = await new Builder()63// .usingServer('http://remoteclient:4444/wd/hub')64// .withCapabilities(capabilities)65// .forBrowser('chrome')66// .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.
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