Tests using Page Factory Design and Page Object Model opens two instances of the browser using Selenium and Java
Asked Answered
N

1

1

I have a test in Selenium WebDriver with Page Object Model, if I run the following test open two windows in the Chrome browser, first window is empty in a URL line. How I can opening only one with onet.pl URL? I trying deleting initialize Chrome driver but will have null.exception error. What am I doing wrong?

package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;

public class LoginPage {
    private WebDriver driver;
    //private WebDriverWait wait;

    public LoginPage() {
        this.driver = new ChromeDriver();
        //wait = new WebDriverWait(driver,10);
    }
    @FindBy(xpath="//div[@class='col-md-12']//form")
    private WebElement loginForm;

    @FindBy(id="username")
    private WebElement loginField;

    @FindBy(id="password")
    private WebElement passwordField;

    @FindBy(xpath="//button[@class='btn btn-multisport-default btn-multisport-large btn-full-width login-button']")
    private WebElement buttonLogin;
    public void open() {
        driver.get("https://onet.pl");
    }
    public void enterLogin() {
        String login = "[email protected]";
        loginField.sendKeys(login);
    }
    public void enterPassword() {
        String password = "Benefit!";
        passwordField.sendKeys(password);
    }
    public void login() {

        buttonLogin.click();
    }
}

And test code:

import Pages.Dashboard;
import Pages.LoginPage;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

import java.util.concurrent.TimeUnit;

public class LogIn {
    @Test
    public void correctLogin() {
        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        loginPage.open();
        loginPage.enterLogin();
        loginPage.enterPassword();
        loginPage.login();
        driver.close();
        driver.quit();
        Dashboard dashboard = new Dashboard(driver);
        dashboard.getLoggedUser();
    }
}
Nolly answered 21/7, 2020 at 20:55 Comment(0)
E
1

From your test class as you are passing the reference of the driver:

LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);

In LoginPage class, within the constructor, instead of:

public LoginPage() {
    this.driver = new ChromeDriver();
    //wait = new WebDriverWait(driver,10);
}

you need to do reuse the instance passed as follows:

public LoginPage(WebDriver loginPageDriver) {
    this.driver=loginPageDriver;
}

That would solve the issue of 2 browsers getting opened.


References

You can find a couple of relevant detailed discussions in:

Embank answered 21/7, 2020 at 22:24 Comment(1)
sure, have a nice day!Nolly

© 2022 - 2025 — McMap. All rights reserved.