0

I'm just started learning how to write automation tests and have NullPointerException when trying to use @ParameterizedTest on a step where I'm trying to create new driver (LoginData login = new LoginData(driver);). Everything working just fine with normal @Test. What I'm doing wrong?

My code:

public class LoginData {

    private WebDriver driver;

    @FindBy(how = How.XPATH, using = "//a[@class = 'close-modal-window']/img")
    private WebElement close;

    @FindBy(how = How.XPATH, using = "//input[@id='email']")
    private WebElement emailField;
    @FindBy(how = How.XPATH, using = "//input[@id = 'password']")
    private WebElement passwordField;
    @FindBy(how = How.XPATH, using = "//button[@class = 'primary-global-button']")
    private WebElement loginButton;

    @FindBy(how = How.XPATH, using = "//app-submit-button/button[@class = 'ubs-primary-global-button']")
    private WebElement signInGoogle;
    @FindBy(how = How.XPATH, using = "//span[@class = 'show-hide-btn']")
    private WebElement showHidePassword;
    @FindBy(how = How.XPATH, using = "//div[@class = 'forgot-wrapper']/a[@class = 'ubs-forgot-password']")
    private WebElement forgotPassword;
    @FindBy(how = How.XPATH, using = "//div[@id = 'email-err-msg']/app-error/div")
    private WebElement errorEmail;
    @FindBy(how = How.XPATH, using = "//div[@id = 'pass-err-msg']/app-error/div")
    private WebElement errorPassword;
    @FindBy(how = How.XPATH, using = "//div[@class = 'missing-account']/p/a[@class = 'ubs-sign-up-link']")
    private WebElement signUpLink;

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

    //("click on 'close' button")
    public LoginData clickCloseIcon() {
        close.click();
        return this;
    }

    //input email | value = {emailInput}
    public LoginData inputEmail(String emailInput) {
        emailField.click();
        emailField.clear();
        emailField.sendKeys(emailInput, Keys.ENTER);
        return this;
    }

    //input password | value = {passwordInput}
    public LoginData inputPassword(String passwordInput) {
        passwordField.click();
        passwordField.clear();
        passwordField.sendKeys(passwordInput, Keys.ENTER);
        return this;
    }

    // click on login button
    public LoginData clickLoginButton () {
        loginButton.click();
        return new LoginData(driver);
    }

}

and test class:

public class LoginDataTest {
    private WebDriver driver;

    final String BASE_URL = "https://SOME_URL/";

    @Before
    public void beforeMethod() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get(BASE_URL);
        driver.findElement(By.partialLinkText("Login")).click();

    }

    @ParameterizedTest
    @ValueSource(strings = {"[email protected]", "[email protected]"})
    public void loginTest(String emailInput) {
        try {
            LoginData login = new LoginData(driver); // here I receive null when using @ParameterizedTest
            login.inputEmail(emailInput).inputPassword("password123").clickLoginButton();
            String actual = driver.getCurrentUrl();
            String expected = "SOME_URL/#/profile/42";
            Assertions.assertEquals(actual, expected);
        } catch (NullPointerException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

      @After
      public void AfterMethod() {
          driver.quit();
      }

}
1
  • You seem to be mixing JUnit 4 annotations with JUnit 5 annotations. Commented Feb 20, 2022 at 11:33

1 Answer 1

1

Use @BeforeEach annotation instead of @Before. @Before annotation is not supported in JUnit5

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.