2

I'm learning Selenium POM with Junit and ran into a problem.

java.lang.NullPointerException
at MainPage.clickSignIn(MainPage.java:27)
at MainPageTest.signInTest(MainPageTest.java:28)

My class:

public class MainPage {
    private WebDriver driver;

    public MainPage(WebDriver driver){
        this.driver = driver;
    }

    @FindBy(xpath = "a[@class='HeaderMenu-link no-underline mr-3']")
    private WebElement signInBtn;
    @FindBy(xpath = "//a[@class='HeaderMenu-link d-inline-block no-underline border border-gray-dark rounded-1 px-2 py-1']")
    private WebElement signUpBtn;
    @FindBy(xpath = "//input[@id='user[login]']")
    private WebElement userNameField;
    @FindBy(xpath = "//input[@id='user[email]']")
    private WebElement userEmailField;
    @FindBy(xpath = "//input[@id='user[password]']")
    private WebElement userPasswordField;
    @FindBy(xpath = "//button[text()='Sign up for GitHub'][1]")
    private WebElement signUpFormButton;

    public LoginPage clickSignIn(){
        signInBtn.click();
        return new LoginPage(driver);
    }

    public SignUpPage clickSignUp(){
        signUpBtn.click();
        return new SignUpPage(driver);
    }

    public SignUpPage clickSignUpForm(){
        signUpFormButton.click();
        return new SignUpPage(driver);
    }

    public MainPage typeUserName(String username){
        userNameField.sendKeys(username);
        return this;
    }

    public MainPage typeUserEmail(String userEmail){
        userEmailField.sendKeys(userEmail);
        return this;
    }

    public MainPage typeUserPassword(String userPassword){
        userPasswordField.sendKeys(userPassword);
        return this;
    }

    public SignUpPage register(String username, String userEmail, String userPassword){
        this.typeUserName(username);
        this.typeUserEmail(userEmail);
        this.typeUserPassword(userPassword);
        this.clickSignUpForm();
        return new SignUpPage(driver);
    }
}

And my test class:

public class MainPageTest {
    private WebDriver driver;
    private MainPage mainPage;
    private LoginPage loginPage;

    @Before
    public void setUp(){
        System.setProperty("webdriver.chrome.driver", "D:\\Java projects\\pageobjectseleniumtest\\src\\main\\resources\\chromedriver.exe");

        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get("https://github.com");
        mainPage = new MainPage(driver);
    }

    @Test
    public void signInTest(){
        LoginPage loginPage = mainPage.clickSignIn();
        String heading = loginPage.getHeadingText();
        Assert.assertEquals("Sign in to GitHub", heading);
    }
}

Help me, please.

2
  • I think this link will help you at this point. stackoverflow.com/questions/17236855/…
    – Meet
    Commented Jan 28, 2020 at 5:03
  • @Meet I have tried with try/catch construction and my test passed, but wasn't click to signIn button. Maybe i don't understand smthg...
    – dimsonQA
    Commented Jan 28, 2020 at 5:59

2 Answers 2

1

I think you are forgeting to init the Elements and try to click on an element that is still null.

Add something like this to the constructor of the page

PageFactory.initElements(driver, this);

Something like:

public MainPage(WebDriver driver){
    this.driver = driver;
    PageFactory.initElements(driver, this);
}

Look at this question for an example: How to Implement Page Object and Page Factory Pattern in Selenium Webdriver?

1
  • 1
    Thank you very much! You have helped me!
    – dimsonQA
    Commented Jan 28, 2020 at 9:37
2

Try to change

@FindBy(xpath = "a[@class='HeaderMenu-link no-underline mr-3']")
    private WebElement signInBtn;

to

@FindBy(xpath = "//a[@class='HeaderMenu-link no-underline mr-3']")
    private WebElement signInBtn;

Two slashes are missing before a.

1
  • 1
    Thank you very much too! You have helped me, i'm blind...
    – dimsonQA
    Commented Jan 28, 2020 at 9:38

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.