Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending PageObject using PageFactory for WebElementFacade #2971

Closed
cbaquero-ar opened this issue Nov 15, 2022 · 4 comments
Closed

Extending PageObject using PageFactory for WebElementFacade #2971

cbaquero-ar opened this issue Nov 15, 2022 · 4 comments

Comments

@cbaquero-ar
Copy link

I'm trying to use PageFactory within a PageObject extending class, but I get a NullPointerException when trying to access a WebElementFacade. Is there any example with PageObjets that you can refer with the complete code of a working PageObject child class?
Here is mine:
`package pageobjects;

import exceptions.InvalidCredentialsLoginException;
import javafx.util.Duration;
import net.serenitybdd.core.annotations.findby.FindBy;
import net.serenitybdd.core.pages.PageObject;
import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.annotations.At;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.WhenPageOpens;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.PageFactory;

import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

@DefaultUrl("http://automation.sgh-os.10.125.125.74.nip.io/auth/login")
@at(urls={"#HOST/auth/login"})
public class LoginPage extends PageObject {

@FindBy(xpath = ".//input[@formcontrolname='username']")
public WebElementFacade user;
@FindBy(xpath = ".//input[@formcontrolname='password']")
private WebElementFacade password;
@FindBy(xpath = ".//*[@translate='auth.login.form.SUBMIT']")
private WebElementFacade loginButton;

@FindBy(xpath = "//p[contains(@class,'ng-star-inserted')]")
private WebElementFacade errorMessage;

public LoginPage() {
    super();
    open();
}

@WhenPageOpens
public void waitUntilPageLoads() {
    setWaitForTimeout(30000);
    user.waitUntilVisible();
    element(password).waitUntilVisible();
    element(loginButton).waitUntilVisible();
}

}
`

The error I get is:
net.serenitybdd.core.pages.UnableToInvokeWhenPageOpensMethods: Could not execute @WhenPageOpens annotated method: null at net.serenitybdd.core.pages.PageObject.callWhenPageOpensMethods(PageObject.java:954) at net.serenitybdd.core.pages.PageObject.initializePage(PageObject.java:888) at net.serenitybdd.core.pages.PageObject.open(PageObject.java:883) at net.serenitybdd.core.pages.PageObject.open(PageObject.java:869) at pageobjects.LoginPage.<init>(LoginPage.java:34) at starter.cucumber.steps.LoginSteps.iLoginAs(LoginSteps.java:21) at ✽.I login as 'AUTOAdmin'(file:///C:/Users/baque/Documents/Trabajo/Lamansys/Proyectos/serenity-cucumber-poc/src/test/resources/features/Login.feature:4) Caused by: java.lang.NullPointerException at pageobjects.LoginPage.waitUntilPageLoads(LoginPage.java:40) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)

@wakaleo
Copy link
Member

wakaleo commented Nov 15, 2022

Try removing the constructor.

@cbaquero-ar
Copy link
Author

cbaquero-ar commented Nov 15, 2022

If I remove the constructor and the call to "open()" method, the browser doesn't start at all. I removed the constructor and moved the open() method outside, but again it gives me the error (NullPointerException when trying to use the WebElementFacade instance).
If I do not have a @WhenPageOpens method, but just call open() and then try to access the WebElementFacade instance, is the same issue.
Any other ideas?

@wakaleo
Copy link
Member

wakaleo commented Nov 16, 2022

You can call the open() method but don't do it in the constructor - that will interfere with the Serenity lifecycle.

@cbaquero-ar
Copy link
Author

cbaquero-ar commented Nov 16, 2022

I found my error: I was calling LoginPage constructor on the Cucumber Step. Removed the constructor and the call to it, then directly used loginPage.open(). Now I can use both WebElement and WebElementFacade with the @FindBy annotation.

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants