top of page

Streamlining Your Selenium Tests with Java Streams and Lambdas


Lambdas

A lambda expression is a microcode that takes in parameter(s) and returns a value. Lambda expressions are similar to methods, but they are anonymous and they can be implemented right in the body of a method.


Lambdas Syntax

1. The simplest lambda expression contains a single parameter and an expression:

parameter->expression

2. To use more than one parameter, wrap them in parentheses:

(parameter1, parameter2)->expression

Example

Use a lambda expression in the ArrayList's forEach() method to print every item in the list:


import java.util.ArrayList;
public class Main{
public static void main(String[] args){
ArrayList<Integer> numbers =new ArrayList<Integer>();     numbers.add(5);     
numbers.add(9);     
numbers.add(8);     
numbers.add(1);
numbers.forEach( (n) -> { System.out.println(n); } );
 }
}

Streams

A stream of objects supports both sequential and parallel aggregate operations. Examples include finding the max or min element and finding the first element matching the giving criteria in a declarative way similar to SQL statements used in SELECT queries.

Streams Syntax


List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");  
 myList     
.stream()     
.filter(s -> s.startsWith("c"))     
.map(String::toUpperCase)     
.sorted()     
.forEach(System.out::println);

Implementation of Java Streams and Lambdas in Selenium WebDriver

Java Lambdas and Streams simplify the Selenium WebDriver code, especially when you are working with web tables, handling windows/frames, sorting the list of web elements, or filtering the web elements from the collection.

Steps for Implementation


Step 1:

Create a Maven Java project to get started with the implementation of Java Streams and Lambdas in Selenium WebDriver. Search for dependencies such as Selenium Java, WebDriver Manager, and TestNG from the Maven repository location https://mvnrepository.com/ and add to pom.xml as shown below.


Step 2:

Create a TestNG class and add the below test methods.


1 , The below test method will find out the list of all the hyperlinks from the demowebshop page and then filter out the links which are having only Linktext.


@Test
public void tc1_filter_links() { 		WebDriverManager.chromedriver().setup(); 	
WebDriver driver = new ChromeDriver(); 		driver.manage().window().maximize();   		driver.get("http://demowebshop.tricentis.com"); 		List<WebElement> links = driver.findElements(By.tagName("a")); 		System.out.println("No of Links :" + links.size()); 		
// Below Code is using Lambdas & Streams 
List<String> filterlink = links
.stream().filter(ele -> !ele.getText()
.equals("")).map(ele -> ele.getText()) 				.collect(Collectors.toList()); 		
System.out.println("After Applying Filter, Links having only Linktext :" + filterlink.size()); 		filterlink.forEach(System.out::println); 		
driver.close();   	
}

Expected Output on Console:

  • No of Links: 95

  • After Applying Filter, Links having only Linktext: 66


2 , The below test method will filter the products by prices from the list of having greater than $1000 from the home page of the demowebshop.


@Test
public void tc2_filter_price() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("http://demowebshop.tricentis.com/");


List<WebElement> prodTitles = driver.findElements(By.xpath("//h2[@class='product-title']"));
List<WebElement> prodPrices = driver.findElements(By.xpath("//div[@class='prices']"));


Map<String, Double> products_map = new HashMap<String, Double>();


for (int i = 0; i < prodTitles.size(); i++) {
	String title = prodTitles.get(i).getText();
	double price = Double.parseDouble(prodPrices.get(i).getText());
     products_map.put(title, price);
}
// Below Code is using Lambdas & Streams// find product whose price is greater than 1000 (Process using filter)
System.out.println("**** Product price is > 1000 using filter & lambda ****");
System.out.println("**** Below Products price is > 1000                ****");
products_map.entrySet().stream().filter(e -> e.getValue() >
1000).forEach(v -> System.out.println(v));
driver.close();
	}

Expected Output on Console:

  • Product price is > 1000 using filter & lambda

  • Below Products price is > 1000

  • Build your own expensive computer=1800.0

  • Build your own computer=1200.0

  • 14.1-inch Laptop=1590.0

3 , The below test method will handle switching to Windows.


@Test
public void tc4_handle_windows()
	{
	WebDriverManager.chromedriver().setup();
	WebDriver driver = new ChromeDriver();
	driver.get("https://demoqa.com/browser-windows");
	driver.findElement(By.id("tabButton")).click();
		
	Set<String> windowIds =driver.getWindowHandles(); 
// Here using Set collection
//Print the url's using lambda expression
windowIds.forEach(winid ->
System.out.println(driver.switchTo().window(winid)
.getCurrentUrl()));
		
		driver.close();
    }

Expected Output on Console:

  • https://demoqa.com/browser-windows

  • https://demoqa.com/sample

Conclusion


The above is a glimpse of the applications of Java Lambdas and Streams in Selenium to simplify the WebDriver code. The above test methods are some of the common scenarios used in the automation of web applications using Selenium WebDriver API, but not limited to these. Java Lambdas and Streams can be used in any kind of scenario on web tables, handling windows/frames, sorting the list of web elements, or filtering the web elements from a collection.


 
 
 

Recent Posts

See All

Comments


bottom of page