In the field of web automation, Selenium is a standout tool for engaging with web applications. Efficient sorting and filtering of data are essential when working with web tables. This blog post delves into the utilization of Selenium in conjunction with Java streams to adeptly sort and filter web tables, presenting a streamlined method for manipulating these tables on the web also helps us in getting a more concise and optimized code.
To learn more about Streams, you can read my previous blog, Unlocking the Power of Java Streams: Simplifying Code with Efficiency.
Understanding the Landscape
Web tables are commonly used to display structured data on web pages. Whether it’s an ecommerce site displaying product information or a data analytics platform presenting metrics, the ability to organize and analyze tabular data is fundamental. Selenium, a widely used web automation tool, allows developers to interact with these web tables and extract the necessary information.
Java streams, introduced in Java 8, provide a concise and expressive way to process data collections. Combining Selenium with Java streams can lead to more readable and maintainable code, making the task of sorting and filtering web tables an elegant process.
Setting Up the Environment
Before delving into the code, ensure you have the necessary tools in place:
Install the Selenium WebDriver: Download the WebDriver for your preferred browser (e.g., ChromeDriver or GeckoDriver for Firefox). Also, we can add dependencies to manage our drivers.
Set up a Java project: Create a new Java project in your favorite IDE and include the Selenium WebDriver libraries in your project.
Add Java 8 or later: Make sure your project is configured to use Java 8 or a later version to take advantage of Java streams.
Understanding the Code
Let us start by sorting a web table using Java Stream. Assume we have a simple table with three columns having various values in each of the columns. As we click on the column having the names of items, the list gets adjusted in a specified order. What we are doing in this code is capturing the elements of the list and comparing it with the sorted list with the help of assertion.
The following code snippet demonstrates how to sort the table based on the column:
import java.util.List;
import java.util.stream.Collectors;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
public class WebTableSortingUsingStreams {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Managing the browser
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//Maximizing the browser
driver.manage().window().maximize();
//Navigating to the URL
driver.get(“https://rahulshettyacademy.com/seleniumPractise/#/offers”);
//Click on Column using the xpath
driver.findElement(By.xpath(“//tr/th[1]”)).click();
//capture all web elements of the column into list again by using xpath.
List<WebElement> elementsList = driver.findElements(By.xpath(“//tr/td[1]”));
//capture text of all webelements into new(original) list, in this we are converting the elements of
//list into a stream format by making the use of .stream method and then mapping them using .map
List<String> originalList = elementsList.stream().map(s- >s.getText()).collect(Collectors.toList());
//sorting method is used on the original list of step 3 -> sorted list
List<String>sortedList = originalList.stream().sorted().collect(Collectors.toList());
//Now, we use assertion to compare original list vs sorted list (both should look same)
Assert.assertTrue(originalList.equals(sortedList));
}
}
In this code, we click on the column to grab the sorted elements, and then we compare those Sorted lists with the original list with the help of assertion.
Conclusion:
This not only elevates code readability but also enhances maintainability, facilitating adaptation to changes in the web table structure. As you delve deeper into the potential of Selenium and Java, consider incorporating these techniques into your web automation projects to streamline table manipulation and unlock new possibilities for data extraction and analysis.
Leave A Comment