top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Automate Sorting and Pagination using Java Streams

Before we start, Please go through this link https://www.numpyninja.com/post/java-stream this blog has covered other concepts of Java streams.


Stream sorted() in Java :-

 Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

Syntax :

Stream<T> sorted()

Where, Stream is an interface and T is the type of stream elements.


 Let’s take an example of Web table.

1. Here in this table, all the veg/fruits names are not in sorted order.

 


 

 

 











In order to sort the order of the names in the web table, Click on the column Veg/fruit name. Then you can see the Sorted list of Veg/fruit names in the web table.

Using Selenium we should verify does it exactly sort in the same order or not.

 

 


 

 












Let’s design an algorithm in order to automate,

 

1.     Click on the column veg/fruits name. 




2.     Capture all the Web Elements into the list. This path is returning multiple results so we need to store it in List.

 3.     Get all the text of all Web elements into new (Original) list. Instead of using loops and iterating through each element, we can use Stream() which support multiple method to manipulate and produce desire Output. Here map() retrives the text of each element in each iteration using streams. And we need to collect all these list of items into a new List using collect() and store it into list using Collectors.toList()

4.  Apply Sorting on the originalList of step 3 using stream and collect the new list and store it in sortedList.

 5.     Compare Orginal List with Sorted list. By using Assertion we can assert original list with Sorted list. 

 











  Console Output:

 


 

 









If you look at the Console output, the original list and sorted list of the items are same. The list of items sorted in the webpage and list of items when sorted method applied on the original list using streams both looks same. So, instead of using for loop and writing code for iterating each element, we can directly use Streams which make code optimized for iterate and getting the text of all the elements. 

 

Exception : If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

 

Let’s see an example for Sorting list of Integer values using Streams.

 

 

 





In the above example, we have Array list of Integers which are in unsorted order. By using Streams sorted() we can sort the above list of integers in an Ascending Sorted order.




Console Output:








Here, we are using for each to Iterate through each value in the list and sorting in the ascending order and printing the value in the console

 

Pagination using Java Streams:

 

 What is Pagination ?





Imagine testing a website presenting products, search outcomes, or various data. Rather than showcasing everything in one go, the content is distributed over several pages. Pagination /Paging facilitates users’ navigation through these pages, commonly employing options like “Next,” “Previous,” or numbered page links.


 How to handle pagination?

There are different approaches to handling pagination on web pages. The basic method involves identifying pagination elements (such as “Next” or page numbers) using appropriate locators (XPath, CSS selectors, etc.) and iterating through a loop to click on each pagination link. Inside the loop, perform the necessary actions (e.g., data extraction, validation) on the current page. But what if the pagination elements are not directly visible? There are more advanced approaches like using dynamic XPath locators of page elements or infinite scrolling using JavaScript executors to load more content.


Lets see an example on Pagination :


1. Click on the column veg/fruits name

 


2. Capture all the Web elements into the list and Scan the veg/fruits name column with get text until condition is true then get the price of Rice as shown below. If the condition is false navigate to next page to find the specific element and to ge the price of it.






In the above code, Using Stream we are iterating through each element for the list of elements. If we have condition to get the text of specific element then use  stream filter () which filter out the details of the element which we need.  Before map() to get the price of the veggie, we have  stream filter() to check the condition of a  specific element for example “Rice” is true or false, if it is true then map() gets the price of Rice and collects that data into list.

Here map(s->getpriceofveggie(s)) is custom and a subsequent method. “s” is the web element that contains the Rice.

In order, to print the price value loop through foreach of the list.


 









Here in the above table, 37 is the following sibling of Rice.




Here in the above example, we are getting the text of following-sibling “Rice” and return the price value.



Console Output :




In the above program, we did sorting  for list of the table elements and storing it in list. Then we are using stream filter method to filter for the specific element “Rice” and get the text of it. Then the filter element is passed into map() to get the price of the “Rice” and then collected into the list. If price is equal to 1, it means the price value of Rice is found. Or if price is lesser than 1, it means the value of rice is not found and need to navigate to next page of the table until if we found the specific element value.  












 Here if you see the Web table, Rice is in the 4th page of the Web table to get the price of Rice. Using pagination concept we can successfully navigate to different pages and can find the specific element from either dynamic web tables or stable web tables.

 

 

 

 

 

 

 

197 views0 comments

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page