2

Seleniumに問題があります。次のテストでは、Webサイトにアクセスし、製品を取り出してカートに追加し、カートに移動する必要があります。その間に、いくつかの要素が表示されるのを待ちます。

私のマシンでは、カートに製品を追加するまでテストは正常に機能します。この後、divはajaxで満たされ、テストはそこで要素を待機する必要があります。要素はブラウザに表示されますが、テストは待機中です。タイムアウトでさえ、それを気にするほど面白くないようです。

wait.until(pause(2));代わり に待つか、driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);すべてを設定すると正常に機能しますが、これは正しい解決策にはなりません。

何か案が?

私のシステム:

MacOS X10.8.2またはWindows7(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;
            }
        };
    }

}
4

2 に答える 2

2

@snieke、セレンバージョンを2.28.0にアップグレードしてみてください。私はそれがあなたのために働くと99%確信しています。

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>2.28.0</version>
</dependency>
于 2012-12-15T12:27:55.417 に答える
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;
        }
    }

}
于 2012-12-13T15:42:10.060 に答える