【问题标题】:Using @FindBy with my own class, not with WebElement将@FindBy 与我自己的类一起使用,而不是与 WebElement 一起使用
【发布时间】:2015-04-27 09:44:24
【问题描述】:

是否可以将@FindBy 不仅用于 WebElement,还可以用于我自己的类?

我想让我的类与页面元素协作并在其中覆盖 WebElement 接口的一些方法。

这样我就实现了这个类:

public class NamedElement implements WebElement {
    public boolean isDisplayed(){
        try{
            return element.isDisplayed();
        } catch (NoSuchElementException noElement) {
            return false;
        }
    }

并且想像这样声明元素:

@FindBy(xpath = ".//a[contains(text(), 'Log in')]")
public NamedElement loginButton;

但我得到了错误:

java.lang.IllegalArgumentException: Can not set lenovo.tests.page.NamedElement field lenovo.tests.page.NCBLoginPage.loginButton to com.sun.proxy.$Proxy9
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:116)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:104)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:91)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:78)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:64)
at lenovo.tests.step.NCBLoginStep.<init>(NCBLoginStep.java:20)

您能否建议我正确的方法来覆盖 WebElement 方法并改用我的 .isDispalyed() 方法?我想继续使用@FindBy 注释。

【问题讨论】:

  • 你重写了WebElement的所有方法吗?
  • 当然,我在覆盖所有方法后收到此错误。

标签: java selenium-webdriver factory-pattern pageobjects findby


【解决方案1】:

这个answer here 正在描述你想要什么。他在this blog 中记录了创建您自己的 PageFactory 的方法——这是要走的路。至少我们(在工作中)正在这样做。我们的目标是对每个 WebElement 方法进行自动重试,而不是讨厌的 StaleElementReferenceExceptions,并在 Chrome 无法自行处理时自动将元素滚动到视图中。 ;-)

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2016-08-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多