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)
PageFactory.initElements(driver, this);is invoked withnulldriver. Variabledriveris not initialized