tt0686
tt0686

Reputation: 1849

Verify Select element

Good afternoon in my timezone

I am using Selenium to test my web application.There is a dropdown list on a page that when we choose one value from it, it will fullfill 3 input text fields and select values in three more dropdown lists. There is a lot of possible combinations to fullfill those fields so i want to use regular expressions to verify the fulfillment.The Dropdown list when selected makes an Ajax call to fullfill all those fields. So i was thinking to use the following statements to make the "assertions":

wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//input[@name='name']"), text)); 

This statement will be used to check the input fields, but i realize that the method "textToBePresentInElement" does not accept regular expression in the place of the text(second argument). Which options do i have ? Because the fullfillment is made through Ajax , i have to wait , one possible solution is to use Thread.sleep while verifying the text through something like this driver.findElement().getText().matches("REgEx"); There is no better solution ?

To check the other 3 dropdown lists what method should i use ? The Thread.sleep following this statement : (new Select(driver.findElement(By.xpath("//select[@name='tipoTransacao']")))).getFirstSelectedOption().getText().matches

Thanks in advance

Best regards

Upvotes: 1

Views: 701

Answers (2)

Ajinkya
Ajinkya

Reputation: 22720

Here is Java solution

public void waitUntilTextIsPresent(WebElement element, String regex, long timeout, long polling) {
        final WebElement webElement = element;
        final String regex = regex;

        new FluentWait<WebDriver>(driver)
        .withTimeout(timeout, TimeUnit.SECONDS)
        .pollingEvery(polling, TimeUnit.MILLISECONDS)
        .until(new Predicate<WebDriver>() {

            public boolean apply(WebDriver d) {
                return (webElement.getText().matches(regex); 
            }
        });
}

Upvotes: 2

Steve Weaver Crawford
Steve Weaver Crawford

Reputation: 1059

A horrible mismash of Java & C# here, due to copy/pasting your examples and my working solution, but hopefully you can adapt it to just Java...

This should wait until the regex matches, you don't always have to use the ExpectedConditions class

public void WaitForTextPresent(By by, string regex, int maxSecondsToWait = 30)
{
    new WebDriverWait(_webDriver, new TimeSpan(0, 0, maxSecondsToWait))
        .Until(d => d.FindElement(by).getText().matches(regex));

}

Upvotes: 0

Related Questions