API Testing : Check pagination information

Verify that the API response contains the correct pagination information.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that we have already set up the Rest Assured framework dependencies in our maven project and imported the necessary libraries2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class APITest {67 @Test8 public void testPaginationInformation(){910 /​/​Assuming that we have the URL to the API endpoint saved in baseURI variable11 baseURI = "http:/​/​api.example.com/​";1213 /​/​Assuming that we have the API endpoint in the path "/​users"14 given().queryParam("page", "2").15 when().get("/​users").16 then().body("page", equalTo(2)).17 body("per_page", equalTo(6)).18 body("total", equalTo(12)).19 body("total_pages", equalTo(2)).20 statusCode(200);2122 /​/​Assuming that we need to connect to remote client with desired capabilities (Chrome browser version 91 and Windows 10 OS)23 DesiredCapabilities capabilities = new DesiredCapabilities();24 capabilities.setBrowserName("chrome");25 capabilities.setVersion("91");26 capabilities.setPlatform(Platform.WINDOWS);2728 RemoteWebDriver driver = null;2930 try{31 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);32 }catch(MalformedURLException e){33 e.printStackTrace();34 }3536 driver.get("https:/​/​www.example.com");37 /​/​Assuming that we need to perform some UI testing on the website in addition to API testing3839 }40}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming an API endpoint URL4const API_ENDPOINT = "https:/​/​example.com/​api";56/​/​Assuming the expected pagination data7const EXPECTED_TOTAL_RECORDS = 100;8const EXPECTED_PAGE_NUMBER = 2;9const EXPECTED_PAGE_SIZE = 20;1011describe("API Pagination Information", function() {12 it("should return the correct pagination information", function(done) {13 /​/​Assuming an API call to get data with given pagination14 const pageNumber = 2;15 const pageSize = 20;16 const apiCall = `${API_ENDPOINT}?pageNumber=${pageNumber}&pageSize=${pageSize}`;1718 /​/​Assuming a GET request using Axios library19 axios20 .get(apiCall)21 .then(function(response) {22 /​/​Assuming the response returns pagination data in the following format:23 /​/​{"totalRecords": 100, "pageNumber": 2, "pageSize": 20}24 const paginationData = response.data.pagination;25 expect(paginationData.totalRecords).to.equal(EXPECTED_TOTAL_RECORDS);26 expect(paginationData.pageNumber).to.equal(EXPECTED_PAGE_NUMBER);27 expect(paginationData.pageSize).to.equal(EXPECTED_PAGE_SIZE);28 done();29 })30 .catch(function(error) {31 /​/​Assuming error handling code32 done(error);33 });34 });3536 /​/​Assuming remote testing capability using Selenium WebDriver37 /​/​const remoteWebDriver = require('selenium-webdriver');38 /​/​const capabilities = require('selenium-webdriver/​lib/​capabilities');3940 /​/​Assuming desired capabilities and remote client details41 /​/​const remoteBrowserName = 'firefox';42 /​/​const remoteBrowserVersion = 'latest';43 /​/​const remotePlatformName = 'Windows 10';44 /​/​const remoteGridURL = 'http:/​/​localhost:4444/​wd/​hub';4546 /​/​Assuming remote driver instance creation47 /​/​const remoteDriver = new remoteWebDriver.Builder()48 /​/​ .withCapabilities(capabilities.remote({49 /​/​ browserName: remoteBrowserName,50 /​/​ version: remoteBrowserVersion,51 /​/​ platform: remotePlatformName52 /​/​ }))53 /​/​ .usingServer(remoteGridURL)54 /​/​ .build();55});

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