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.
Non-static version:
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.
Static version
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();
This does not work, and results in a java.lang.NullPointerException on the line marked with "*" (the line accessing the WebElement).
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.