14

下面的代码片段可以正常工作,但是我在使用该wait.until()行时遇到了一些问题:

wait.until(new ElementPresent(By.xpath("//a[@title='Go to Google Home']")));

它有效,但我想发送我的PageFactory WebElement homePageLink

wait.until(new ElementPresent(homePageLink));

有没有办法做到这一点?

这些新奇的 Selenium 2 功能让我有点头晕,我找不到太多文档。

谢谢。

public class GoogleResultsPage extends TestBase {

    @FindBy(xpath = "//a[@title='Go to Google Home']")
    @CacheLookup
    private WebElement homePageLink;

    public GoogleResultsPage() {  
        wait.until(new ElementPresent(By.xpath("//a[@title='Go to Google Home']")));
        assertThat(driver.getTitle(), containsString("Google Search"));
    }  
}

public class ElementPresent implements ExpectedCondition<WebElement> {

    private final By locator;

    public ElementPresent(By locator) {
        this.locator = locator;
    }

    public WebElement apply(WebDriver driver) {
        return driver.findElement(locator);
    }
}
4

3 回答 3

21

我将PageFactoryAjaxElementLocatorFactory一起使用- PageFactory 是您正在使用的 Selenium 2 Page Objects 模式的支持类,而 AjaxElementLocatorFactory 是元素定位器的工厂。在您的情况下,构造函数将如下所示:

public GoogleResultsPage() { 
    PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
}

此代码将等待最多 15 秒,直到注释指定的元素出现在页面上,在您的情况下,homePageLink 将由 xpath 定位。您将不需要使用 ElementPresent 类。

于 2010-07-19T10:22:01.050 回答
2

有一个在 C# 上实现的请求。

这里是:

IWebDriver driver = new ChromeDriver();
RetryingElementLocator retry = new RetryingElementLocator(driver, TimeSpan.FromSeconds(5));
IPageObjectMemberDecorator decor = new DefaultPageObjectMemberDecorator();
PageFactory.InitElements(retry.SearchContext, this, decor);
于 2016-04-26T11:39:01.537 回答
1

AjaxElementLocatorFactory 在内部使用 SlowLoadableComponent。在这里查看源代码

于 2013-02-04T11:40:48.443 回答