0

I'm trying to have a page object class with just the elements inside it. but when using a instance from the steps (testclass) I can't use them. the Xpaths are correct, if I try to call them inside my Steps it works.

If possible I just wanna call them and handle on Steps like: loginScreen.loginUser.sendKeys("userX");

Here is my PageObject:

import com.test.core.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class LoginScreen extends BasePage {

    public String baseUrl = "https://10.125.103.235:6501/opt/security_check";

    @FindBy(xpath = "//input[@id='userId']")
    public WebElement loginUser;

    @FindBy(xpath = "//input[@id='password']")
    public WebElement loginPassword;

    @FindBy(xpath = "//input[@id='loginButton']")
    public WebElement loginEnter;

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

    public void set_user(String user){
        loginUser.sendKeys(user);
    }

    public void set_password(String password){
        loginPassword.sendKeys(password);
    }

    public void clickOnEnter(){
        loginEnter.click();
    }

}

And here my Steps

import com.test.core.BaseTest;
import com.test.screens.LoginScreen;
import io.cucumber.java.en.And;
import io.cucumber.java.pt.Dado;

public class LoginSteps extends BaseTest{

    LoginScreen loginScreen = new LoginScreen(getDriver());

    @Dado("que o usuario acesse o PSRM")
    public void browserOpenPsrm() {
        loginScreen.driver.manage().window().maximize();
        loginScreen.driver.get(loginScreen.baseUrl);
    }

    @And("realiza o login com o USER: {string} e SENHA: {string}")
    public void loginSuccess(String user, String senha) {
        loginScreen.set_user(user);
        loginScreen.set_password(senha);
        loginScreen.clickOnEnter();

    }
}

error:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:70)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:39)
    at com.sun.proxy.$Proxy19.sendKeys(Unknown Source)
    at com.test.screens.LoginScreen.set_user(LoginScreen.java:32)
    at com.test.stepdefinitions.LoginSteps.loginSuccess(LoginSteps.java:23)
2
  • Post your BasPage.java file code Commented Sep 30, 2022 at 16:08
  • PageFactory.initElements(driver, this); is invoked with null driver. Variable driver is not initialized Commented Oct 3, 2022 at 9:12

1 Answer 1

1

Issue Line: LoginScreen loginScreen = new LoginScreen(getDriver());

In your 'LoginSteps', you have initialised your non-static LoginScreen object on class level. Hence, after class loading, initialization of the class takes place which means initializing all static members of the class.* Pleas, initialise your loginScreen object inside a Before method so that it gets initialised when class is loaded.

LoginScreen loginScreen;
        
@Before
public void beforeScenario(){
    loginScreen = new LoginScreen(getDriver());
    System.out.println("This will run before the Scenario");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.