API Testing : Check sorting order

Verify that the API response contains the correct sorting order based on the provided sort parameter.

Language: Java

Framework: Rest assured

copy
1import static io.restassured.RestAssured.*;2import static org.hamcrest.Matchers.*;34public class APITest {56 @Test7 public void testSortOrder() {8 String sortParam = "name"; /​/​Assuming that the sorting parameter is "name"9 given().param("sort", sortParam)10 .when().get("https:/​/​example.com/​api/​endpoint")11 .then().assertThat().statusCode(200)12 .and().body("data", contains("value1", "value2", "value3")); /​/​Assuming that the returned data is an array and has values in correct sorting order13 }1415 /​/​ Uncomment below code to run the test on a remote client with desired capabilities1617 /​/​ @Test18 /​/​ public void testSortOrderOnRemoteClient() {19 /​/​ String sortParam = "name"; /​/​Assuming that the sorting parameter is "name"20 /​/​ DesiredCapabilities capabilities = new DesiredCapabilities(); /​/​Assuming required capabilities are already set21 /​/​ RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);22 /​/​ driver.get("https:/​/​example.com/​api/​endpoint?sort=" + sortParam);23 /​/​ /​/​ Assert the sorting order as mentioned above24 /​/​ }2526}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumption: The API endpoint is already known and can be accessed for testing45const chai = require('chai');6const assert = chai.assert;7const request = require('request');89/​/​Local driver connection10describe('API sorting order test', function() {11 it('should return correct sorting order', function(done) {12 13 /​/​Insert the API endpoint14 const apiEndpoint = 'https:/​/​example.api.com';1516 /​/​Insert the sort parameter for testing17 const sortParam = 'date';1819 /​/​Insert the expected sorting order20 const expectedOrder = ['2022-01-01', '2022-01-08', '2022-02-01', '2022-02-10'];2122 request.get(apiEndpoint + '?sort=' + sortParam, function(error, response, body) {23 assert.equal(response.statusCode, 200);2425 /​/​Assuming the response data is in JSON format26 const responseData = JSON.parse(body);27 const sortedData = responseData.data.sort((a, b) => new Date(a.date) - new Date(b.date));28 const sortedDataDatesOnly = sortedData.map(data => data.date.substring(0, 10));29 assert.deepEqual(sortedDataDatesOnly, expectedOrder);3031 /​/​Remote driver connection using desired capabilities32 /​/​Note: Assumes the Selenium WebDriver is already installed and running on the remote client machine33 /​/​const webdriver = require('selenium-webdriver');34 /​/​const capabilities = webdriver.Capabilities.chrome();35 /​/​const chromeOptions = {'args': ['--no-sandbox', '--disable-dev-shm-usage', '--headless']};36 /​/​capabilities.set('chromeOptions', chromeOptions);37 /​/​const driver = new webdriver.Builder()38 /​/​ .usingServer('http:/​/​<REMOTE CLIENT IP>:4444/​wd/​hub')39 /​/​ .withCapabilities(capabilities)40 /​/​ .build();4142 done();43 });44 });45});

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