0% found this document useful (0 votes)
22 views

Note 19 - Java Stream & Lambda Exp

The document describes code for testing a web application using Selenium. It initializes the ChromeDriver, navigates to a URL, clicks on a column header, captures element text into lists, sorts and compares the lists, filters elements to find and print the price of an item.

Uploaded by

dwivedia670
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Note 19 - Java Stream & Lambda Exp

The document describes code for testing a web application using Selenium. It initializes the ChromeDriver, navigates to a URL, clicks on a column header, captures element text into lists, sorts and compares the lists, filters elements to find and print the price of an item.

Uploaded by

dwivedia670
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package tests;

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;

public class LiveDemo {

public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("https://rahulshettyacademy.com/greenkart/#/offers");

// click on column

driver.findElement(By.xpath("//tr/th[1]")).click();

// capture all webelements into list

List<WebElement> elementsList = driver.findElements(By.xpath("//tr/td[1]"));

// capture text of all webelements into new(original) list

List<String> originalList = elementsList.stream().map(s ->


s.getText()).collect(Collectors.toList());

// sort on the original list of step 3 -> sorted list


List<String> sortedList =
originalList.stream().sorted().collect(Collectors.toList());

// compare original list vs sorted list

Assert.assertTrue(originalList.equals(sortedList));

List<String> price;

// scan the name column with getText ->Beans->print the price of the Rice

do

List<WebElement> rows = driver.findElements(By.xpath("//tr/td[1]"));

price = rows.stream().filter(s -> s.getText().contains("Rice"))

.map(s -> getPriceVeggie(s)).collect(Collectors.toList());

price.forEach(a -> System.out.println(a));

if(price.size()<1)

driver.findElement(By.cssSelector("[aria-label='Next']")).click();

}while(price.size()<1);

private static String getPriceVeggie(WebElement s) {

// TODO Auto-generated method stub

String pricevalue = s.findElement(By.xpath("following-sibling::td[1]")).getText();

return pricevalue;

You might also like