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?
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.