首页 文章

验证选择元素

提问于
浏览
0

我的时区下午好

我正在使用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 :( new Select(driver.findElement(By.xpath("//select[@name='tipoTransacao']")))).getFirstSelectedOption().getText().matches

提前致谢

最好的祝福

2 回答

  • 0

    这是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

    由于复制/粘贴您的示例和我的工作解决方案,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));
    
    }
    

相关问题