Skip to content
CodeKruPro

Pagination

37/62

Pagination

APIs paginate large result sets. The search endpoint supports page and limit parameters.

The response includes meta with pagination info: page, per_page, total, last_page.

Task: Request page 1 with 3 results per page (no filters) and print the pagination metadata.

Expected output:

Page: 1
Per page: 3
Total: 15
Last page: 5
Results on page: 3

About this lesson

REST Assured pagination test: page, limit and the meta block

Learn REST AssuredIntermediateQuery Parameters and Search

How it works

Large collections are served in slices. A client asks for a page number and a page size, and the server answers with that slice plus metadata so the caller knows how many pages exist. On testkru /search the two inputs are page and limit, and the meta object reports page, per_page, total and last_page. The last page is the total divided by the limit, rounded up, and never less than one.

The server clamps bad input instead of rejecting it. A limit below one becomes one, a limit above fifty becomes fifty, and a page below one becomes the first page. Asking for a page beyond the end is not an error either: the status is still 200, data is an empty array and meta still shows the real total and last page. That makes paging easy to get wrong silently, which is exactly why it deserves tests.

Each queryParam() call adds one more pair, and you can chain as many as you need. Passing an int is fine because the value is an Object. To count the records actually returned, read data as a list with getList() and ask for its size().

About the assignment

Request the first page of unfiltered results with three items per page and report every pagination field together with the number of records on that page.

What you will do

  1. Chain two queryParam() calls on given(): page set to 1 and limit set to 3.
  2. Send a GET to /search.
  3. Read meta.page, meta.per_page, meta.total and meta.last_page with getInt() and print them after Page:, Per page:, Total: and Last page:.
  4. Read data with getList(), take its size(), and print it after Results on page:.

Common mistakes

  • Reading per_page as perPage

    The key uses an underscore. A camel-case path does not exist, so getInt() finds nothing to unbox and throws a NullPointerException. Copy keys from a real response rather than from the naming style of your Java code.

  • Treating an empty page as a failure of the request

    Page 99 still answers 200 with empty data. If your loop over pages stops only on an error status it will never stop. Stop when the page number passes last_page or when data comes back empty.

  • Expecting an error for a huge limit

    A limit of 500 is quietly reduced to 50 and per_page reports the reduced value. Assert on per_page from the response, not on the number you sent, when the API documents a cap.

In a real test suite

A thorough pagination check walks every page, collects the ids into a set and asserts that the set size equals meta.total, which catches duplicates and gaps between pages. Edge cases worth one test each are the last partial page, a page past the end, and the smallest and largest allowed limits.

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /search with queryParam "page" = 1 and "limit" = 3
        // TODO: Print all meta fields and count results on this page
    }
}