验证选择元素

时间:2014-02-25 16:58:15

标签: java select selenium selenium-webdriver

我的时区下午好

我正在使用Selenium来测试我的Web应用程序。页面上有一个下拉列表,当我们从中选择一个值时,它将填充3个输入文本字段并在另外三个下拉列表中选择值。 有很多可能的组合来填充这些字段,所以我想使用正则表达式来验证实现。选择下拉列表会调用Ajax来填充所有这些字段。 所以我想用以下语句来制作“断言”:

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

此语句将用于检查输入字段,但我意识到方法“textToBePresentInElement”不接受正则表达式代替文本(第二个参数)。我有哪些选择? 因为fullfillment是通过Ajax完成的,所以我必须等待,一个可能的解决方案是使用Thread.sleep,同时通过类似driver.findElement().getText().matches("REgEx");之类的内容验证文本 没有更好的解决方案?

要检查其他3个下拉列表,我应该使用哪种方法? 此声明后面的Thread.sleep: (新Select(driver.findElement(By.xpath("//select[@name='tipoTransacao']")))).getFirstSelectedOption().getText().matches

提前致谢

祝你好运

2 个答案:

答案 0 :(得分:1)

这是Java解决方案

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); 
            }
        });
}

答案 1 :(得分:0)

Java和Java的可怕错误C#在这里,由于复制/粘贴您的示例和我的工作解决方案,但希望您可以将其改编为Java ...

这应该等到正则表达式匹配,你不必总是使用ExpectedConditions类

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));

}