Skip to main content

Quality Assurance

Unleashing the Power of Custom Selenium Methods: A Guide to Stream Mapping

Extending General Link for Experience Editor Mode in Sitecore

In the ever-evolving landscape of web automation, Selenium has emerged as a powerful tool for testers and developers alike. One key aspect of Selenium’s flexibility lies in its ability to accommodate custom methods, offering a clear approach to handling complex scenarios. In this blog, we will delve into the realm of developing custom Selenium methods using the dynamic and expressive capabilities of stream mapping.

Building a Foundation

Before we dive into the complexities of stream mapping in Selenium, let’s establish a foundational understanding of the principles involved. Familiarity with Java and Selenium basics is recommended to fully grasp the concepts discussed in this guide.

Catch up on our previous blogs:

https://blogs.perficient.com/2024/01/03/unlocking-the-power-of-java-streams-simplifying-code-with-efficiency/

https://blogs.perficient.com/2024/02/07/streamlining-web-table-manipulation-with-selenium-and-java-streams/

Understanding Stream Mapping

Stream mapping, an integral feature of Java, provides a concise and expressive way to manipulate data collections. Leveraging this functionality in Selenium allows developers to create custom methods seamlessly interacting with web elements, providing a streamlined and efficient automation process.

We can see an example of custom build selenium using a stream mapper in the example mentioned below:

 

List<String>price= elementsList.stream().filter(s>s.getText().contains("Beans")). map(s>getPriceVeggi(s)).collect(Collectors.toList());
price.forEach(s->System.out.println(s));


}

private static String getPriceVeggie(WebElement s) {
String pricevalue= s.findElement(By.xpath("following-sibling::td[1]")).getText();
return pricevalue;
    

Explanation

This code snippet exemplifies our effective utilization of methods. We begin by converting elements into a stream, followed by filtering values based on the ‘contains’ specification. This process allows us to send only the values of specific elements rather than the entire stream, subsequently mapping the price of the contained values.

In scenarios requiring certain conditions to be met, we apply the filter method as usual to refine the values. Consequently, this approach precisely isolates the desired elements. The ‘getPriceVeggi’ method, situated at the bottom, is dedicated to crafting a logic that retrieves the value of a specific bean.

Input

package practice;

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));




//It scans the names in a column with getText (for example, it might find 'Beans'), then prints the price of that specific element using the
// 'getPriceVeggi' method. This method, in turn, creates its own new method where we specify the XPath of the WebElement.

List<String>price= elementsList.stream().filter(s->s.getText().contains("Beans")).map(s->getPriceVeggie(s)).collect(Collectors.toList());
price.forEach(s->System.out.println(s));


}

//A new method has been crafted for "getPriceVeggi," serving its purpose within stream manipulation to identify the specific WebElement among a list of WebElements
private static String getPriceVeggie(WebElement s) {
String pricevalue= s.findElement(By.xpath("following-sibling::td[1]")).getText();
return pricevalue;
}

}

Output

38

Benefits of Custom Selenium Methods with Streams Mapping

Code Readability

Stream mapping promotes concise and readable code, making it easier for developers and testers to understand and maintain automation scripts.

Reusability

Custom methods developed using stream mapping can be reused across different projects, reducing redundancy, and enhancing the efficiency of your automation framework.

Adaptability

Stream mapping allows for the dynamic adaptation of automation scripts to changes in web elements, ensuring the longevity and resilience of your Selenium-based projects.

Conclusion

Incorporating custom Selenium methods through the utilization of stream mapping opens a world of possibilities for web automation enthusiasts. By combining the power of Selenium with the expressive nature of Java stream, developers and testers can create robust, adaptable, and efficient automation scripts. Embrace the flexibility stream mapping offers, and elevate your Selenium automation projects to new heights of sophistication and effectiveness.

Happy Reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Smriti Rai

Smriti Rai works in quality assurance at Perficient. She has over two years of experience and currently works on Adobe technologies. Smriti is a coding enthusiast and enjoys learning new technology and automation tools. She constantly strives for opportunities to learn new things and develop her knowledge.

More from this Author

Categories
Follow Us