Home > Back-end >  Can't create driver using selenium and junit5 with ParameterizedTest
Can't create driver using selenium and junit5 with ParameterizedTest

Time:02-22

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();
      }

}

CodePudding user response:

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

  • Related