What is pagination?
Most of the ecommerce applications split the content across multiple pages to improve user experience especially when there are huge number of records to display. To navigate from one page to another we click on the next button until navigating to the last page.
Here is step by step guide to handle pagination using Selenium in java:
Step 1: Create a new maven project in Eclipse IDE as shown below:
Click on Next button to configure the Maven project:
Enter the Group ID and Artifact ID and then click finish button.
A new project is created with the below project structure:
Step 2: Add the required dependencies in pom.xml.
In the below example, selenium-java and TESTNG are added as shown below:
Step 3 : Create a class named PaginationConcept.java in the required package.
Step 4: Initialize WebDriver and navigate to the webpage: Create an instance of the WebDriver and navigate to the webpage where pagination exists.
Below is a screenshot of the webpage with the table and Page navigation:
Step 5: Next step is to identify the Web table and the number of elements in the table in First Page.
Read the number of elements in the first (current) page to an ArrayList as shown below:
Step 6: Locate and Identify the Next Button to navigate to next page. Load the elements in the navigated page.
Continue this process until the last page is reached. The last page is identified when the Next button is disabled and no further pages to navigate.
Step 7: ArrayList now contains all the rows in table across the pages.
The number of rows in the table can be determined by the size of ArrayList.
Step 8: Final step is to close the WebDriver instance to clean up the resources.
Note:
Its important to implement the appropriate error handling to handle scenarios such as :
Table doesn’t exist in the web page.
No rows in the table.
Enough number of rows are not available to present the Next page.