Pages
- Home
- Manual Testing Tutorials
- Manual Testing Materials
- Manual Testing Interview Q & A
- ISTQB
- UNIX /Linux
- SQL
- Agile Methodology
- Selenium with Java
- Selenium with Python
- Automation Testing Materials
- API Testing
- Advanced Java
- Cypress Tutorials
- ETL Testing Documents
- ETL Testing videos
- Big Data Hadoop
- SDET Essentials
- Miscellaneous Topics
- Career Guidance
- Mock Interviews
- Resume Templates
- YouTube Videos
- Udemy Courses
- Online Training
Apache POI Tutorials for Selenium Automation
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();
}
}
Popular Posts
- How To Explain Project In Interview Freshers and Experienced
- Selenium Frequently Asked Questions & Answers Part-6
- API/Webservices Testing using RestAssured (Part 1)
- How to use HashMap in Selenium WebDriver
- Java Programs for Selenium
- Manual & Automation Testing Free Video Tutorials | YouTube Playlists
- ETL Test Scenarios and Test Cases
- How to Generate Extent Report Version 4 in TestNG Framework
- Python Interview Questions and Answers Part-1