简体   繁体   English

@FindBy批注定义的Webelement返回空指针

[英]Webelement defined by @FindBy annotation returns null pointer

For some reason, when I call method Spage.editExButton(int ID), I get an error saying that WebElement first is null. 出于某种原因,当我调用方法Spage.editExButton(int ID)时,出现一条错误消息,指出WebElement首先为null。 Why is it null? 为什么它为空? I have defined it using the @FindBy annotation. 我已经使用@FindBy批注定义了它。 I have to explicitly define the element using findElement(By.id("xxx")) in order to be able to click on it. 为了能够单击它,我必须使用findElement(By.id(“ xxx”))明确定义该元素。 But why am I unable to call it using the @FindBy notation? 但是,为什么我不能使用@FindBy表示法来调用它?

public class SPage extends GPage<SPage> {

    public SPage() {
        super();
    }

    public SPage(String pageType) {
        super(pageType);
    }

    @FindBy(id = "xxx")
    WebElement first;

    public WebElement eButton(int ID) {
        first.click();
        String tmp = ID + "-Edit";
        WebElement edit = getDriver().findElement(By.id(tmp));
        return edit;
    }

    public EPage cEdit(int ID) {
        eButton(ID).click();
        return new EPage(getBasePageType()).openPage(EPage.class);
    }
}

I am calling the method like this: 我正在这样调用方法:

static EPage epage;
static SPage spage;

@Test
public void edit_exception() {
             epage = spage.cEdit(IDbefore);
}

You need to call this (preferably in your constructors): 您需要调用此函数(最好在构造函数中):

PageFactory.initElements(getDriver(), this);

More information: https://code.google.com/p/selenium/wiki/PageFactory 详细信息: https : //code.google.com/p/selenium/wiki/PageFactory

As all other answer mentions that we have to initialize webElements using PageFactory.initElements() , 正如所有其他答案提到的那样,我们必须使用PageFactory.initElements()来初始化webElements,

There are two ways of doing it or rather two places where it can be done : 有两种方法可以完成,或者可以在两个地方完成:

1) inside class SPage : 1)在类SPage中

As there are two constructors in SPage class we have to add following code to initailize all the elements declared in this class, it should be done in both class because we don't know that which constructor will be used to initialize SPage : 由于SPage类中有两个构造函数,因此我们必须添加以下代码以使该类中声明的所有元素完整化,因此应在两个类中都完成此操作,因为我们不知道将使用哪个构造函数来初始化SPage

We will be passing driver instance to SPage class constructors, which will look like below : 我们将驱动程序实例传递给SPage类构造函数,如下所示:

public SPage(WebDriver driver) {
    super();
    PageFactory.initElements(driver, this);
}

public SPage(String pageType, WebDriver driver) {
    super(pageType);
    PageFactory.initElements(driver, this);
}

2) inside Other classes where SPage WebElements are to be used : 2)在要使用SPage WebElement的其他类中

in the above example elementsCan be initialzed in the class where edit_exception() method is written, in short before anyplace we want to use elements/actions of the class SPage , Now the code will look like below: 在上述例子中elementsCan在edit_exception()方法被写入的类中initialzed,总之之前的任何地方,我们要使用的元件/类S页面的动作,现在代码将看起来像下面:

@Test
public void edit_exception() {
             spage =  PageFactory.initElements(driver, SPage.class);  //we are not passing driver instance to SPage class
             epage = spage.cEdit(IDbefore);
}

In my test class I added the following line of Code 在测试类中,我添加了以下代码行

WebMainMenu mainmenu = PageFactory.initElements(driver, WebMainMenu.class); WebMainMenu mainmenu = PageFactory.initElements(driver,WebMainMenu.class);

mainmenu.doStuff(driver, 5); mainmenu.doStuff(driver,5);

etc 等等

I agree as per above that you need to instantiate the page object. 如上所述,我同意您需要实例化页面对象。

WebElement first;

is null because element is not initialize when we are instantiating our page class. 为null,因为在实例化页面类时未初始化element。 So initialize all elements in page class 因此,初始化页面类中的所有元素

 PageFactory.initElements(driver,  this); 

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

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