英文:
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
只返回翻译好的部分:
-
你可以在
launchWebsite()
内的driver = initializeDriver();
上设置断点,然后逐步执行代码,你会看到问题所在。你应该花一些时间学习如何调试自己的问题,这样你就可以更熟练地找到和解决自己的问题。 -
问题在于你两次调用了
launchWebsite()
。 -
你在
submitOrder()
测试的第一行再次调用了它。 -
我建议移除
@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.
-
You have
launchWebsite()
marked as@BeforeMethod(alwaysRun=true)
@BeforeMethod(alwaysRun=true) public LandingPage launchWebsite()
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论