在使用Selenium和Java进行UI自动化时,打开2个Chrome浏览器窗口。

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

2 Chrome Browsers opening during UI automation using Selenium and Java

问题

在运行submitOrder方法时,你遇到了两个浏览器窗口被打开的问题。要解决这个问题,你可以在DefaultTest类中使用@BeforeClass注解来确保只有一个浏览器被初始化,而不是在每个测试方法之前初始化一个新的浏览器。这样可以确保在整个测试类中只有一个浏览器实例。

这是修改后的DefaultTest类的示例:

import org.testng.annotations.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

public class DefaultTest {
    
    public WebDriver driver;
    public LandingPage landingPage;
    
    // Method to initialize driver
    @BeforeClass
    public void initializeDriver() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.manage().window().maximize();
    }
    
    // Method to open the browser and website before each test
    @BeforeMethod(alwaysRun=true)
    public LandingPage launchWebsite() {
        landingPage = new LandingPage(driver);
        landingPage.goTo();
        return landingPage;
    }
    
    // Method to close browser after each test
    @AfterMethod(alwaysRun=true)
    public void tearDown() {
        driver.close();
    }
}

通过这种方式,浏览器将在DefaultTest类的@BeforeClass方法中初始化,然后在每个测试方法之间共享同一个浏览器实例,从而解决了多次打开浏览器的问题。

英文:

I have 3 classes as below

`public class SubmitOrderTest extends DefaultTest {

@Test
public void submitOrder()
{
	LandingPage landingPage = launchWebsite();
	landingPage.login("standard_user", "secret_sauce");
	String errorMessage = landingPage.getErrorMessage();
}

}`

`public class DefaultTest {

public WebDriver driver;
public LandingPage landingPage;

//Method to initialize driver
public WebDriver initializeDriver() 
{
	driver=new ChromeDriver();
	driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
	driver.manage().window().maximize();
	return driver;
}


//Method to open the browser and website before each test
@BeforeMethod(alwaysRun=true)
public LandingPage launchWebsite() 
{
	driver = initializeDriver();
	landingPage = new LandingPage(driver);
	landingPage.goTo();
	return landingPage;
}

//Method to close browser after each test
@AfterMethod(alwaysRun=true)
public void tearDown()
{
	driver.close();
}

`

`public class LandingPage extends AbstractComponents {

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

@FindBy(id = "user-name")
WebElement userName;

@FindBy(id = "password")
WebElement userPassword;

@FindBy(id ="login-button")
WebElement loginButton;

@FindBy(xpath = "//h3[@data-test='error']")
WebElement errorMessage;


public void goTo()
{
	driver.get("https://www.saucedemo.com/");
}

public void login(String email, String password)
{
	userName.sendKeys(email);
	userPassword.sendKeys(password);
	loginButton.click();
}

public String getErrorMessage()
{
	waitForElementToAppear(errorMessage);
	return errorMessage.getText();
	 
}

}`

When running the subOrder method, 2 browsers are opening.

When running the code, my expected output was

Using method launchWebsite

1)Chrome browser opened
2)Url entered

Using method login

3)User name entered
4)Password entered
5)Login button clicked

Using method tearDown

6)Close browser

But the output I am getting

Using method launchWebsite

1)Chrome browser1 opened
2)Url entered

Again one more chrome browser 2 is opened and url is entered

Using method login

3)User name entered on chrome browser 2
4)Password entered on chrome browser 2
5)Login button clicked on chrome browser 2

Using method tearDown

6)Closed browser2

What changes should I make, so that only one browser is opened?

答案1

得分: 0

只返回翻译好的部分:

  1. 你可以在launchWebsite()内的driver = initializeDriver();上设置断点,然后逐步执行代码,你会看到问题所在。你应该花一些时间学习如何调试自己的问题,这样你就可以更熟练地找到和解决自己的问题。

  2. 问题在于你两次调用了launchWebsite()

  3. 你在submitOrder()测试的第一行再次调用了它。

  4. 我建议移除@BeforeMethod(alwaysRun=true)注解,因为你需要在每个测试中使用LandingPage的返回实例。

英文:

If you put a breakpoint on driver = initializeDriver(); inside launchWebsite() and the step through the code, you'd see exactly what the problem is. You should spend some time learning to debug your own problems so that you can become more proficient at finding and fixing your own issues.

The problem is that you are calling launchWebsite() twice.

  1. You have launchWebsite() marked as @BeforeMethod(alwaysRun=true)

    @BeforeMethod(alwaysRun=true)
    public LandingPage launchWebsite()
    
  2. then you call it again in the first line of the submitOrder() test.

I would remove the @BeforeMethod(alwaysRun=true) annotation because you are going to need the return instance of LandingPage inside of each test.

huangapple
  • 本文由 发表于 2023年4月20日 10:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76060014.html
匿名

发表评论

匿名网友

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

确定