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

huangapple go评论106阅读模式
英文:

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

问题

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?
我在Selenium WebDriver中使用Page Object Model编写了一个测试,但如果运行以下测试,会在Chrome浏览器中打开两个窗口,第一个窗口是一个空的URL行。我如何只打开一个带有onet.pl URL的窗口?

I trying deleting initialize Chrome driver but will have null.exception error.
我尝试删除初始化Chrome驱动程序,但会出现null.exception错误。

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;

    public LoginPage() {
        this.driver = new ChromeDriver();
    }

    @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 = "vadim1234@test.pl";
        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();
    }
}
英文:

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 = "vadim1234@test.pl";
        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();
    }
}

答案1

得分: 1

从您的test类中,因为您传递了driver的引用:

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

LoginPage类中,在构造函数内,不要使用:

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

而是需要重用传递的实例,如下所示:

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

这将解决打开两个浏览器的问题。

英文:

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:

huangapple
  • 本文由 发表于 2020年7月22日 04:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63022955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定