4

我正在使用 jBehave/Selenium 进行自动化测试。

现在我使用以下代码来获取页面上的可见元素;

public WebElement getVisibleElement( final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod ) {
       return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until( new Function<WebDriver, WebElement>(){
                public WebElement apply(WebDriver driver) {
                    try{
                        WebElement element = parentElement.findElement(by);
                        if ( element.isDisplayed() ) {
                            return element;
                        } else {
                            return null;
                        }
                    } catch( NoSuchElementException e ) {}

                    return null; 
                }
        } );
    }

现在的问题是,如果页面上不存在该元素,Selenium 会花费大量时间尝试在页面上找到它。有什么方法可以优化代码,以便在这种情况下不会花费很长时间?

4

2 回答 2

0

首先,我认为您不应该通过调用 findElement 并捕获潜在异常来检查元素的存在。最好使用 findElements 并检查列表大小

public WebElement getElementIfPresent(By locator) {
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
    List<WebElement> elements = driver.findElements(locator);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    if(!elements.isEmpty()) {
        return elements.get(0);
    }
    else {
        return null;
    }
}

问题是 WebDriver 将隐式等待元素出现。因此,在执行上述代码之前,您可能希望通过将隐式等待时间设置为较低值来缩短该时间。在对可能不存在的元素进行任何查找之前,这需要是 dobe。然后需要恢复原来的隐式等待值,否则可能会影响剩余测试的稳定性。

isDisplayed()最后,您只需使用您在示例中所做的那样检查可见性。

于 2013-10-15T13:34:53.317 回答
0

甚至更优化的解决方案 - 是使用findElements(locator).size()...

    /**
     * Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
     */
    private WebElement waitForElement(By by) {
        int attempts = 0;
        int size = driver.findElements(by).size();

        while (size == 0) {
            size = driver.findElements(by).size();
            if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
                                                             by.toString(),
                                                             MAX_ATTEMPTS));
            attempts++;
            try {
                Thread.sleep(1000); // sleep for 1 second.
            } catch (Exception x) {
                fail("Failed due to an exception during Thread.sleep!");
                x.printStackTrace();
            }
        }

        if (size > 1) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

        return driver.findElement(by);
    }

它的构建方式是在对其进行操作之前调用它。就像是,

WebElement myElement = waitforElement(By.cssSelector("input#someInput"));
myElement.sendKeys("something");

这是一个经过验证的解决方案,我已经在生产级回归测试系统中进行了测试并积极使用。

于 2013-10-15T13:49:39.977 回答