繁体   English   中英

在我自己的类中使用@FindBy,而不是WebElement

[英]Using @FindBy with my own class, not with WebElement

不仅可以将@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批注。

这里的答案描述了您想要的。 他在此博客中记录了创建自己的PageFactory的方法-这是要走的路。 至少我们(在工作中)确实做到了这一点。 我们的目标是对每个WebElement方法进行自动重试,而不是讨厌的StaleElementReferenceExceptions并在Chrome无法自行处理的情况下自动将其滚动到视图中。 ;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM