Search code examples
javaseleniumwait

wait until no text in a box / boxes


I am writing in java a method using selenium that will clear a form entry, that is, several boxes may have text in them. My method will click a "clear search form" and then wait for all the boxes to be clear. I tried the following:

		WebDriverWait wt = new WebDriverWait(driver, 30);
		click("Clear Search Form", clearSearchForm);
		for (int ind = 0; ind < boxes.size(); ind++) {
			wt.until(ExpectedConditions.textToBePresentInElementValue(boxes.get(ind), ""));
		}
                                             

however, it looks like the Expected Conditions waits for the text to contain "" rather than to equal "", so any text will contain "". I forgot to put in the clearSearchForm click, and the method succeeded anyway even though the text boxes were not clear, since they all contained "".

I tried to find an ExpectedConditions for the value to be equal to "" and not contain it but I could not.

I have seen people rewrite a wait method so they could customize it to what they want. When I searched with google I could not find examples, though I am sure there are examples, as I have seen them before. I just can't find them now. Can anyone direct me to such an example (or just type one)?

Thanks


Solution

  • Here is a possible solution

    public static ExpectedCondition<WebElement> textInValue(final WebElement el) {
        return new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver driver) {
                return el.getAttribute("value").equals("") ? el : null;
            }
        }
    }
    

    And use it like this

    for (int ind = 0; ind < boxes.size(); ind++) {
        try {
            wt.until(textInValue(boxes.get(ind)));
        }
        catch (TimeoutException e) {
            // print message
        }
    }