简体   繁体   English

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

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

Is it possible to use @FindBy not only with WebElement, but with my own class? 不仅可以将@FindBy与WebElement一起使用,还可以与我自己的类一起使用吗?

I would like to have my class for cooperating with page elements and to override some methods of WebElement interface in it. 我想让我的类与页面元素合作,并在其中重写WebElement接口的某些方法。

So that I implemented the class: 这样我实现了该类:

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

And would like to declare the element like this: 并想这样声明元素:

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

But I get the error: 但是我得到了错误:

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)

Could you please advice me the proper way to override WebElement methods and to use my .isDispalyed() method instead? 您能否建议我重写WebElement方法并改用.isDispalyed()方法的正确方法? I would like to continue using the @FindBy annotation. 我想继续使用@FindBy批注。

This answer here is describing what you want. 这里的答案描述了您想要的。 He documented the way to create your own PageFactory in this blog - which is the way to go. 他在此博客中记录了创建自己的PageFactory的方法-这是要走的路。 At least we (at work) are doing exactly that. 至少我们(在工作中)确实做到了这一点。 Our goal is to have an automatic retry on every WebElement method instead of nasty StaleElementReferenceExceptions and scroll an element automatically into view, if Chrome cannot handle it on its own. 我们的目标是对每个WebElement方法进行自动重试,而不是讨厌的StaleElementReferenceExceptions并在Chrome无法自行处理的情况下自动将其滚动到视图中。 ;-) ;-)

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

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