Suppose I have the following code:
public int getNumOfPostInstancesByTitle(String postMainTitle) {
int numOfIns = 0;
List<WebElement> blogTitlesList = driver.findElements(blogTitleLocator);
for (WebElement thisBlogTitle : blogTitlesList) {
String currentTitle = thisBlogTitle.getText();
if (currentTitle.equalsIgnoreCase(postMainTitle)) {
numOfIns++;
}
}
return numOfIns;
}
what is the proper way converting it with predicate lambda?
You can calculate your numOfInts with a simple combination of map, filter and count :
return driver.findElements(blogTitleLocator)
.stream()
.map(WebElement::getText) // convert to a Stream of String
.filter(s -> s.equalsIgnoreCase(postMainTitle)) // accept only Strings
//equal to postMainTitle
.count(); // count the elements of the Stream that passed the filter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With