Apache POI Tutorials for Selenium Automation


 
Apache POI Tutorial Part1 - How To Read Data From Excel 


Apache POI Tutorial Part2 - How To Write Data on Excel 


Apache POI Tutorial Part3 - How To Read Data from Formula Cell in Excel Sheet


Apache POI Tutorial Part4 - How To Write Formula Cell in Excel Sheet



Apache POI Tutorial Part5 - How To Read Data from Password Protected Excel 


Apache POI Tutorial Part6 - How To Fill Background and Foreground Colors into Cell


Apache POI Tutorial Part7 - MS-Excel with Java Hash Map



Apache POI Tutorial Part8 - How To Read Data from Database and Write into Excel


Apache POI Tutorial Part9 - How To Read Data from Excel and Write into Database Table 


Apache POI Tutorial Part10 - Data Driven Testing in Selenium | TestNG DataProvider and Excel



Apache POI Tutorial Part11 - How To Write WebTable data into Excel Sheet in Selenium (Web Scraping)


Apache POI Tutorial Part12 - How to work with Date Cells in the MS-Excel




Code Snippets:
Github Link : https://bit.ly/3nQsN9L


How to use Java Collections, Lambda Expressions & Streams in Selenium Automation


Example1:

/1) Find Number of Links in Page

2) Print Link Texts from all the links

3) Check how many links does not have href attribute(broken links)

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 io.github.bonigarcia.wdm.WebDriverManager;

public class Demo1 {

public static void main(String[] args) {

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

driver.get("http://demowebshop.tricentis.com/");

List<WebElement> links = driver.findElements(By.tagName("a")); // Here is List is collection

System.out.println(links.size());

//Printing linkTexts using for..each loop(Before Java8)

for (WebElement link : links) {

System.out.println(link.getText());

}

//Printing linkTexts using lambda expression

links.forEach(link -> System.out.println(link.getText()));

//Processing elements using stream -> filter

long workingLinks=links.stream().filter(link->link.getAttribute("href")!=null).count();

System.out.println("Working link:"+workingLinks);

driver.close();

}

}


Example2:

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Demo2 {

public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

driver.get("https://www.amazon.in/");
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.flipkart.com/");//driver.navigate().to("https://www.flipkart.com/");
Set<String>windowIds=driver.getWindowHandles(); // Here using Set collection

for(String windowid:windowIds)
{
driver.switchTo().window(windowid);
System.out.println(driver.getTitle()); //System.out.println(driver.getCurrentUrl());
}
//Print the titles using lambda expression
windowIds.forEach(winid ->      System.out.println(driver.switchTo().window(winid).getTitle()));
driver.quit();
}

}


Example3:
1) Display books in sorted order(A-Z) using DropDown
2) Capture all the products in to a list (Original list)
3) Sort products ( using stream & lamda)then capture in to another list (Sorted list)
4) Compare Original list with Sorted list 


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.openqa.selenium.support.ui.Select;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Demo3 {

public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

driver.get("http://demowebshop.tricentis.com/");
driver.findElement(By.xpath("//ul[@class='top-menu']//a[normalize-space()='Books']")).click();
Select sortbydrp=new Select(driver.findElement(By.id("products-orderby")));
sortbydrp.selectByVisibleText("Name: A to Z");
List<WebElement> product_items=driver.findElements(By.xpath("//h2[@class='product-title']"));
List<String> beforesort=product_items.stream().map(item->item.getText()).collect(Collectors.toList());
List<String> aftersort=product_items.stream().map(item->item.getText()).sorted().collect(Collectors.toList());
System.out.println(beforesort);
System.out.println(aftersort);
if (beforesort.equals(aftersort)) 
            System.out.println("products displayed in Sorted Order"); 
        else
        System.out.println("products displayed in NOT Sorted Order"); 
driver.quit();
}

}


---------------------------------------------------------
Example4:

 1) capture all product titles & prices in hashmap
 2) find product whose price is greater than 800
 3) Sort products based on prices

import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;

public class Demo4 {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://Drivers//chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver(); 
driver.get("http://demowebshop.tricentis.com/");
driver.manage().window().maximize();
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);
}
//Printing titles & prices using for..each loop
System.out.println("**** Printing titles & prices using for..each loop**** ");
for (Map.Entry<String,Double> entry : products_map.entrySet()) {
  System.out.println(entry.getKey()+":"+entry.getValue());
}
// Printing titles & prices using Hashmap .forEach() & Lamda expression
System.out.println("**** Printing titles & prices using lamda expression**** ");
products_map.forEach((t, p) -> System.out.println(t + " : " + p));
//find product whose price is greater than 800 (for..each loop)
System.out.println("**** Product price is > 800 ****");
for (Map.Entry<String,Double> entry : products_map.entrySet()) {
  if(entry.getValue()>800)
  {
  System.out.println(entry.getKey()+":"+entry.getValue());
  }
}
//find product whose price is greater than 800 (Process using filter)
System.out.println("**** Product price is > 800 using filer & lambda ****");
products_map.entrySet().stream().filter( e -> e.getValue() > 800).forEach(v->System.out.println(v));
}

}



Followers