Stream API is the major feature introduced in Java 8. As an automation tester, if you think you don’t have to learn about it, you may be wrong. Yes you don’t need to know everything but little basic knowledge of Stream API will help you to reduce lines of code in Selenium script.
I am starting a series of posts on Usage of Java Stream API in Selenium. In this post, we will see how Stream API can help you to retrieve the text of a List of WebElements quickly.
Introduction of Java Stream API :-
Java 8 provided a new package called java.util.stream which contains multiple classes and interfaces which helps you to get sequence of data from a Collection or array and apply aggregate operation on those elements.
A generic interface named Stream<T> is a major interface in stream package of Java. It provides a sequence of elements supporting sequential and parallel aggregate operations. It contains a lot of methods like map(), count(), distinct(), filter() etc which makes complex operation on elements easier.
Let’s see the usage in a scenario with Selenium:-
We need to locate and print the names of all clothes present at home page of a shopping site. Let’s do the same thing using traditional way first:-
Without using stream API:-
package RealtimeValdationAutomationTesting; import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class RetrieveTextFromListOfElement { @Test public void RetrieveTextFromListOfElementWithoutStremAPI() { // Auto setup of chrome executable file WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Load the URL driver.get("http://automationpractice.com/index.php"); // Locating all product names at home page ListallProductNameElement = driver.findElements(By.xpath("//ul[contains(@class,'active')]//a[@class='product-name']")); // Print count of product found System.out.println("Total product found : "+allProductNameElement.size()); // Logic to iterate webelement to retrieve text and store List allProductNames = new ArrayList<>(); for(WebElement ele : allProductNameElement) { String name = ele.getText().trim(); allProductNames.add(name); } // Printing product names System.out.println("All product names are : "); for(String s :allProductNames) { System.out.println(s); } // closing the browser driver.quit(); } }
Output:-
Total product found : 7 All product names are : Faded Short Sleeve T-shirts Blouse Printed Dress Printed Dress Printed Summer Dress Printed Summer Dress Printed Chiffon Dress PASSED: RetrieveTextFromListOfElementWithoutStremAPI
Using Java Stream API:-
Above , we first stored the found web elements in to a list and then iterated. Using stream we do not need to save it. We just need text from each webelement not web elements. We can get stream of web elements using stream() method. Once we get it, we apply logic to retrieve and add text from each web element in to resultant list in forEach() method. I am using lambda expression in forEach() method.
package RealtimeValdationAutomationTesting; import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class RetrieveTextFromListOfElementUsingStreamAPI { @Test public void RetrieveTextFromListOfElementWithStremAPI() { // Auto setup of chrome executable file WebDriverManager.chromedriver().setup(); // Launching browser WebDriver driver = new ChromeDriver(); // Load the URL driver.get("http://automationpractice.com/index.php"); ListallProductNames = new ArrayList<>(); // Locating all product names at home page /* * We do not need to store list of web elements as well. We can get the stream of found web elements * and apply aggregate function forEach(). Logic behind forEach is to get the text of each web element * and add to list. We are using lambda expression inside forEach. */ driver.findElements(By.xpath("//ul[contains(@class,'active')]//a[@class='product-name']")) .stream() .forEach(product -> allProductNames.add(product.getText())); // Print count of product found System.out.println("Total product found : "+allProductNames.size()); // Printing product names System.out.println("All product names are : "); allProductNames.forEach(name -> System.out.println(name)); // closing the browser driver.quit(); } }
Output:-
Total product found : 7 All product names are : Faded Short Sleeve T-shirts Blouse Printed Dress Printed Dress Printed Summer Dress Printed Summer Dress Printed Chiffon Dress
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
ordering of tutorials is very confusing, from Rest API we moved suddenly to Selenium. Can we please have indexes