Verify that the API correctly handles pagination and returns the correct resources for each page.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import io.restassured.path.json.valida.jsonPathValidator;5import static io.restassured.RestAssured.given;6import static org.hamcrest.Matchers.equalTo;78public class TestAPIPagination {910 @Test11 public void testPaginationHandling() {12 13 //Assuming baseURI and basePath of API endpoints14 RestAssured.baseURI = "http://localhost:8080";15 RestAssured.basePath = "/api/v1";16 17 //Pagination parameters, assuming page size is 1018 int currentPage = 1;19 int pageSize = 10;20 21 //Assuming endpoint for resource and query parameters for pagination22 String endpoint = "/resources";23 String queryParam = "page=" + currentPage + "&size=" + pageSize;24 25 //Requesting resources for first page26 Response response = given().when().get(endpoint + "?" + queryParam);27 28 //Assuming response code for successful API call29 response.then().statusCode(200);30 31 //Assuming response JSON has 'totalElements' key32 response.then().body("totalElements", equalTo(100));33 34 //Assuming response JSON has 'content' key and its size is equal to page size35 response.then().body("content.size()", equalTo(pageSize));36 37 //Connecting to remote server with desired capabilities38 /*DesiredCapabilities capabilities = new DesiredCapabilities();39 capabilities.setCapability("platform", "Windows 10");40 capabilities.setCapability("browserName", "chrome");41 capabilities.setCapability("version", "latest");42 capabilities.setCapability("name", "APIPaginationTest");43 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);*/4445 }46}
Language: Javascript
1// Mocha and Chai.23//Assumptions: 4//1. Pagination takes place in the query parameters and is of the form "?page=x&limit=y" where x and y are integers.5//2. The API returns resources as an array of objects.6//3. The API endpoint for pagination is "https://example.api.com/resource/?page=x&limit=y".78const { expect } = require('chai');9const fetch = require('node-fetch');1011describe('API Pagination Test', () => {12 it('should return the correct resources for each page', async () => {13 const baseUrl = 'https://example.api.com/resource'; 14 const pageLimit = 10; //Assuming each page contains a maximum of 10 resources15 const totalResources = 30; //Assuming there are a total of 30 resources16 17 //Sending a request to the API to retrieve the first page18 const response = await fetch(`${baseUrl}?page=1&limit=${pageLimit}`);19 const responseBody = await response.json();20 21 //Checking if the API returns an array of objects22 expect(responseBody).to.be.an('array').that.is.not.empty;23 24 //Checking if the response contains the expected number of resources25 expect(responseBody.length).to.equal(pageLimit);26 27 //Iterating through the remaining pages and comparing the resources with the expected ones28 for(let i = 2; i <= Math.ceil(totalResources/pageLimit); i++) {29 //Sending a request to the API to retrieve the next page30 const nextPageResponse = await fetch(`${baseUrl}?page=${i}&limit=${pageLimit}`)31 const nextPageResponseBody = await nextPageResponse.json();32 33 //Checking if the API returns an array of objects34 expect(nextPageResponseBody).to.be.an('array').that.is.not.empty;35 36 //Checking if the response contains the expected number of resources37 //If the last page has fewer resources, checking for its actual length instead38 const expectedLength = i === Math.ceil(totalResources/pageLimit) ? totalResources % pageLimit : pageLimit;39 expect(nextPageResponseBody.length).to.equal(expectedLength);40 41 //Checking if the resources on the page match the expected ones42 const expectedResources = responseBody.slice((i-1)*pageLimit, i*pageLimit);43 expect(nextPageResponseBody).to.deep.equal(expectedResources);44 }45 });46});4748//Assuming if a remote client is being used, the desired capabilities are being set externally and will be injected into the WebDriver instance. Code snippet for that is:49// const { Builder } = require('selenium-webdriver');50// const { Options } = require('selenium-webdriver/chrome');51// const capabilities = {52// 'browserName': 'chrome',53// 'platformName': 'Windows 10',54// 'browserVersion': 'latest'55// }56// const options = new Options();57// options.addArguments('--disable-dev-shm-usage');58// options.addArguments('--no-sandbox');59// const driver = new Builder().withCapabilities(capabilities).setChromeOptions(options).build(); //Connecting to remote client with desired capabilities.
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