java.lang.NullPointerException problem in Selenium Test



  • 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.



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2