将selenium测试用例组合到JUnit测试套件中时的不确定行为

时间:2012-10-23 15:09:47

标签: selenium webdriver junit4 test-suite

我有一个场景,我有29个测试用例。所有测试用例都会弹出新的firefox窗口并在独立运行时运行完成。但是,当我将测试用例(全部29个)组合到一个测试套件中时,我得到了“无法定位元素”的随机错误。如果我多次运行测试套件,我可以看到不同的测试用例在不同的地方随机出现故障。注意 - 在点击它们之前,我等待大约100秒的每个元素可见性。代码看起来像 -

    WebElement myDynamicElement = (new WebDriverWait(driver, 100))
    .until(new ExpectedCondition<WebElement>() {
    @Override
    public WebElement apply(WebDriver driver) {
    return driver.findElement(element);
    }

有人可以建议吗?是否推荐使用较小的测试套件?

1 个答案:

答案 0 :(得分:1)

到目前为止还遇到过类似的问题类型。 恕我直言尝试使用更强大的等待机制fluentWait

public WebElement fluentWait(final By locator){
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
                    .ignoring(org.openqa.selenium.NoSuchElementException.class);
            WebElement foo = wait.until(
                    new Function<WebDriver, WebElement>() {
                        public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                        }
                    }
            );
            return  foo;              }     ;

这是documentation在流利的等待

用法如:

String cssSelectorElement ="blablablab";
String xPathElement = "balblabla";

fluentWait(By.cssSelector(cssSelectorElement)).click()
//fluentWait(By.cssSelector(cssSelectorElement)).getText();

fluentWait(By.xpath(xPathElement)).click()
//fluentWait(By.xpath(xPathElement)).getText();

不要忘记在您工作的IDE(IDEA,Eclipse,NetBeans)中逐步调试。