Java和Selenium:Page对象中的静态方法 [英] Java and Selenium: Static methods in Page Objects

查看:117
本文介绍了Java和Selenium:Page对象中的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在页面对象中使用静态方法时,我遇到了NullPointerExceptions的麻烦.如果我使用非静态方法执行此操作,则效果很好.

I'm having trouble with NullPointerExceptions when I try using static methods in a page object. If I do it with non-static methods, it works fine.

非静态版本:

public class ComplaintPage {

    private ExtendedWebDriver driver;

    @FindBy(css = "[data-selector=date-received-complaint]")
    public WebElement dateComplaintReceoved;

    public ComplaintPage() {
        driver = Browser.extendedDriver();
        PageFactory.initElements(driver, this);
    }

    public void setComplaintDate() {
        dateComplaintReceoved.sendKeys(LocalDate.now().toString());
    }
}

Calling code:

ComplaintPage complaintPage = new ComplaintPage;
complaintPage.setComplaintDate();

这很好.日期字段已设置.

This works fine. The date field is set.

静态版本

public class ComplaintPage {

    private static ExtendedWebDriver driver;

    @FindBy(css = "[data-selector=date-received-complaint]")
    public static WebElement dateComplaintReceoved;

    public ComplaintPage() {
        driver = Browser.extendedDriver();
        PageFactory.initElements(driver, this);
    }

    public void static setComplaintDate() {
*       dateComplaintReceoved.sendKeys(LocalDate.now().toString());
    }
}

Calling code:

ComplaintPage.setComplaintDate();

这不起作用,并在标有"*"的行(访问WebElement的行)上导致java.lang.NullPointerException.

This does not work, and results in a java.lang.NullPointerException on the line marked with "*" (the line accessing the WebElement).

我有点喜欢在测试中使用像这样的静态方法,因为我并没有真正看到它的问题,它使代码更易于阅读.而且我以前在C#/VS中做到了,但是由于某种原因,我在这里错过了一些重要的东西.

I kind of like using static methods like this in test, since I don't really see a problem with it, and it makes the code even more easy to read. And I've done it before, in C#/VS, but for some reason I'm missing something important here.

推荐答案

NullPointerException.您会看到,在创建ComplaintPage类的实例时,您正在调用其构造函数:

NullPointerException is thrown because of how PageFactory works. You see, when you create an instance of ComplaintPage class, you are invoking its constructor:

public ComplaintPage() {
        driver = Browser.extendedDriver();
        PageFactory.initElements(driver, this);
    }

构造函数调用PageFactory类的initElements方法.此方法使用Java Reflection API初始化所有WebElement>字段.它基本上将默认的null值更改为接口的实现.它还提供了WebElement的惰性实例化,这意味着-仅在需要时-调用WebElement时才查找(查找?).

The constructor calls initElements method of PageFactory class. This method initializes all of WebElement and List<WebElement> fields with Java Reflection API. It basically changes the default null values to implementations of the interface. It also provides sort of Lazy instantiation of the WebElement which means - WebElements are found (looked for?) only when needed - when you invoke operations on them.

创建静态方法和静态WebElement时-您未调用类的构造函数,这导致无法调用initElements方法.

When you created static methods and static WebElements - you did not call the constructor of the class, which lead to NOT invoking the initElements method.

所有用@FindBy注释的元素都未初始化.这就是为什么将PageFactory与静态方法一起使用不是一个好主意.

All of the elements annotated with @FindBy were not initialized. That's why it's not a good idea to use PageFactory with static methods.

除了使用PageFactory之外,您还可以在静态方法中找到具有经典driver.findElement的元素.

Instead of using PageFactory you can just find the element with classic driver.findElement inside the static method.

    public void static setComplaintDate(WebDriver driver) {
        driver.findElement(By.cssSelector("[data-selector=date-received-complaint]")).sendKeys(LocalDate.now().toString());
    }

这篇关于Java和Selenium:Page对象中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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