WebDriverWait.until(ExpectedConditions.elementToBeClickable(someAjaxMagic))永远不会返回

时间:2012-12-13 13:26:08

标签: java ajax selenium webdriver wait

我遇到了Selenium的问题。在下面的测试中,它应该去网站,拿一个产品,把它添加到购物车并转到购物车。在它们之间,它等待出现一些元素。

在我的机器上,测试工作正常,可以将产品添加到购物车中。在此之后,div用ajax填充,测试应该等待那里的元素。该元素出现在浏览器中,但测试仍在等待。即使超时似乎也不足以打扰它。

如果我等待wait.until(pause(2));或设置driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);,一切正常,但这不是正确的解决方案。

有什么想法吗?

我的系统:

MacOS X 10.8.2或Windows 7(64位)

Firefox 17.0.1

Java 1.6,JUnit 4.10,selenium-api-2.25.0,selenium-support-2.25.0,selenium-firefox-driver-2.25.0,guava-12.0

我的测试:

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;

import javax.annotation.Nullable;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public class TestSomeWebsite {

    @Test
    public void test() throws Exception {
        FirefoxDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 30);

        driver.get("http://www.obi.de");

        // take first product
        driver.findElement(By.cssSelector("p.product_desc > a")).click();

        // add to cart
        wait.until(elementToBeClickable(By.id("addtocart")));
        driver.findElement(By.id("addtocart")).submit();

        // go to cart
        /** everything fine by now **/
        wait.until(elementToBeClickable(By.partialLinkText("Zum Warenkorb")));
        /** we never come to this point :( **/
        driver.findElement(By.partialLinkText("Zum Warenkorb")).click();
    }

    private Function<WebDriver, Object> pause(final int time) {
        return new Function<WebDriver, Object>() {
            int count = 0;

            @Nullable
            public Object apply(@Nullable WebDriver input) {
                count++;
                return count >= time;
            }
        };
    }

}

2 个答案:

答案 0 :(得分:2)

@snieke,尝试将您的selenium版本升级到2.28.0。我99%肯定能为你效劳。

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>2.28.0</version>
</dependency>

答案 1 :(得分:1)

你是否得到超时异常,你应该在30秒后得到它。 你可以尝试:

try {
    wait.until(elementToBeClickable(By.partialLinkText("Zum Warenkorb")));
} catch (TimeoutException e) {
    // Do not stop the test, continue and see what happens
    LOG.info("Timed out", e);
}
driver.findElement(By.partialLinkText("Zum Warenkorb")).click();

它应该在30秒后继续

编辑:

然后尝试编写自己的条件,这就是我所拥有的(你需要调整它以满足你的需求):

public class WebElementIsVisible implements ExpectedCondition<Boolean> {

    private WebElement element;
    private String elementId;

    public WebElementIsVisible(WebElement elementId) {
        this.element = elementId;
    }

    public WebElementIsVisible(String elementId) {
        this.elementId = elementId;
    }

    @Override
    public Boolean apply(WebDriver webDriver) {
        if (element != null) {
            return isElementVisible(element, webDriver);
        } else if (elementId != null) {
            return isElementIdVisible(elementId, webDriver);
        } else {
            throw new RuntimeException("Not supposed to reach here");
        }
    }

    private Boolean isElementIdVisible(String elementId, WebDriver webDriver) {
        try {
            webDriver.findElement(By.id(elementId));
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private Boolean isElementVisible(WebElement element, WebDriver webDriver) {
        try {
            webDriver.findElement(By.id(element.getAttribute("id")));
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

}