3

Im trying to set up cucumber over my page object. I currently have as my login page object:

@FindBy (id  = cF.username)           public WebElement username;
@FindBy (id  = cF.password)           public WebElement password;
@FindBy (id  = cF.loginButton)        public WebElement loginButton;


@Test
public websiteProspector_beta doLogin(String myUsername, String myPassword) throws IOException {
    logs.InfoDetails("Logging Into Tomcat8- 2092");

    try {
        username.sendKeys(myUsername);
        password.sendKeys(myPassword);
        loginButton.click();
        {...}

and my step description as:

    @And("^I log into ao$")
public void i_log_into_ao() throws Throwable {
    actonLogin = new actonLogin(driver);
    actonLogin.doLogin(cF.loginUsername, cF.loginPassword);
    throw new PendingException();
}

When i run it, it successfully runs the first two steps, however when it comes to an object with @findby, I get this error:

java.lang.NullPointerException
at com.acton.PageObjects.loginPage.actonLogin.doLogin(actonLogin.java:41)
at com.acton.cucumber.cucumberSteps.i_log_into_ao(cucumberSteps.java:41)
at ✽.And I log into ao(com/acton/cucumber/login.feature:6)

The reason why I know it's due to the @Findby is because when I change the login page object to use "driver.findelement(By...." it's successful with no errors. is there something im missing?

I am new to cucumber and automation all together, any help i can get is appreciated.

2 Answers 2

5

Did you run PageFactory.initElements? The PageFactory magic won't actually parse the annotations until you do so. I usually put into the constructor a line like:

PageFactory.initElements(driver, this);

So that it runs when I construct the page.

0

You are mapping the elements correctly, however, you forgot to start the WebElements, you do this using PageFactory.initElements(). Below is an example of how I do it in my projects, the selectors come from the LoginActions class.

package money.flowmy.testing.PageObjects;

import money.flowmy.testing.Actions.LoginActions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

public class LoginPage extends LoginActions {

    public static String url = "http://nginx/login";
    public WebDriver webDriver;

    public LoginPage(WebDriver webDriver) {
        this.webDriver = webDriver;
        PageFactory.initElements(webDriver, this);
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.