如何将 selenium 2 PageFactory init Elements 与 Wait.until() 一起使用? [英] How to use selenium 2 PageFactory init Elements with Wait.until()?

查看:20
本文介绍了如何将 selenium 2 PageFactory init Elements 与 Wait.until() 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码片段运行良好,但我在使用 wait.until() 行时遇到了一些问题:

The code snippet below works fine, but I'm having a little trouble with the wait.until() line:

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

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

It works but I want to send my PageFactory WebElement homePageLink instead:

wait.until(new ElementPresent(homePageLink));

有没有办法做到这一点?

Is there any way to do that?

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

These new fangled Selenium 2 features have got my head in a bit of a spin and I can't find much documentation.

谢谢.

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);
    }
}

推荐答案

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

I use PageFactory with AjaxElementLocatorFactory - PageFactory is a support class for the Selenium 2 Page Objects pattern which you are using, and the AjaxElementLocatorFactory is the factory for the element locators. In your case the constructor will looks like:

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

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

This code will wait maximum of 15 seconds until the elements specified by annotations will appear on the page, in your case the homePageLink which will be located by xpath. You will not need to use ElementPresent class.

这篇关于如何将 selenium 2 PageFactory init Elements 与 Wait.until() 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆