I have methods in the test that work correctly, but when the pages end it returns false. How do I make sure that after the method is running, true is returned?
I know that I need to fix the logic in the initNextPageButton method but I don't understand how
public List<String> collectResultsFromPage() {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='_8v6CF']")));
List<WebElement> resultsNameWebElement = driver.findElements(By.xpath("//article//h3[@data-zone-name = 'title']//span"));
return resultsNameWebElement.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
}
private boolean initNextPageButton() {
try {
goNextPageButton = driver.findElement(By.xpath("//a[contains(@class, '_3OFYT')]"));
return true;
} catch (NoSuchElementException e) {
return false;
}
}
public void collectResults() {
findResults = collectResultsFromPage();
while (initNextPageButton()) {
goNextPageButton.click();
findResults.addAll(collectResultsFromPage());
}
}
CodePudding user response:
I believe there are multiple ways to do this, here I would do it using findElements
(plural) to check the list size, if it contains more than zero element then go ahead, return true or else return false.
private boolean initNextPageButton() {
List<WebElement> goNextPageButton = driver.findElements(By.xpath("//a[contains(@class, '_3OFYT')]"));
if(goNextPageButton.size() > 0){
goNextPageButton.get(0).click();
return true;
}
else
return false;
}