0

I have an issue that I am facing, I want to create a function that waits until the text "Other" exists in List. I want it to wait 30 seconds, and in every 5 seconds it will do the call again and validate the new list. (Maximum 5 - 6 times to validate if the text exists in the new list ). I have done the validation by converting the List into map and check any match (the map converting and the checking is working).

The problem is with the wait mechanism, I try to create complex fluent wait that will also receive boolean in case of finding the expected text and then the wait should stop. But I do not figure out what to return if it is not found the text for example in the first or second tries, and I want it to still wait and made the call again and validate again.

  • I want the wait to stop only in two cases:

    • the word "Other" exists - in this case isExists is true.
    • the the time passes is more than 30 seconds.with pulling interval of 5 seconds and the word "other" not displayed

Is there a way to set in the condition of the wait to wait for Boolean == true ?I am not sure that the condition Boolean>() is correct and can be done here in the wait.until , and if it will work

public void testrIntegrationfluent ()
{
    WebElement element = BasePage.getWebElementByXPathWithWaitToClicable("//nz-select[@formcontrolname='selectedIntegrationTypes']/div");
    element.click();
    WebDriver driver2 = WebDriverMgr.getDriver();

    Wait wait = new FluentWait(driver2)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    wait.until(new Function<WebDriver , Boolean>() {
        public Boolean apply (WebDriver driver2) {

                List<WebElement> dropdownOptions = driver2.findElements(By.xpath("//ul[contains(@class, 'ant-select-dropdown-menu')]/li"));
                Boolean  isExists = dropdownOptions.stream().map(WebElement::getText).anyMatch(text -> "Other".equals(text));
                if (isExists.equals(true)) {
                    return isExists;
                }
        return  ****** need to think what to put thatkeep waiting and perform  call for list and validation reoccured ******
    }

});
}
Bastian
  • 1,089
  • 7
  • 25
  • 74
  • Is this not working? Your code will wait for 30 seconds and it will check the boolean value true or not. It should work. Are you getting any error? – Yosuva Arulanthu Oct 05 '19 at 09:30
  • Does return false not work? If you return false, your `wait.until` function should continue waiting. – CEH Oct 05 '19 at 13:52
  • is this Boolean>() meaning that it wait for the return value will be true? – Bastian Oct 06 '19 at 10:46

2 Answers2

0

You could simplify your code as such -- using the FluentWait you declared. You could replace your entire wait.until(new Function<WebDriver , Boolean>() block containing the apply function with just this line. Rather than calling the mapping to get text for all the li elements and checking for text other, you can write your function to look directly for li with text Other:

fluentWait.until
    (ExpectedConditions.presenceOfElement(By.xpath("//ul[contains(@class, 'ant-select-dropdown-menu')]/li[text()='Other'])));

This code will wait until li containing text Other is present.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • but the xpath is for 30 web elements. Is presenceOfElement will check for all the text, in all this elemets? or just search for the first elemet. – Bastian Oct 06 '19 at 10:41
  • Using just `presenceOfElement`, the wait will stop once a single element has been located. If you want to wait for all elements located, you can swap out `presenceOfElement` with `presenceOfAllElementsLocatedBy` and this will wait for all of them. – CEH Oct 06 '19 at 15:06
0

After doing your suggestions return false solved it

Bastian
  • 1,089
  • 7
  • 25
  • 74