Question

I'm trying to run two methods that belong to same class in parallel. My code launches two separate browsers, but when the first method completes, instead of killing that particular browser, it kills the other browser.

Method A - Browser A - Say it takes 20 seconds

Method B - Browser B - Say it takes 40 seconds

When Method A is done, it kills Browser B instead of Browser A.

Here is my code:

public class AccountTest {

    private WebDriver driver;

    @BeforeMethod(alwaysRun=true)
    public void initializeDriver() throws MalformedURLException {
        driver = new FirefoxDriver();
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

    @Test(groups="Login")
    public void loginTest() {
        HomePage homePage = new HomePage(driver);
        SignInPage signInPage = homePage.signIn();
        signInPage.signIn("test_user0401@fakemail.com", "abcd1234");
    }


    @Test(groups="Register")
    public void registerTest() {
        HomePage homePage = new HomePage(driver);
        SignInPage signInPage = homePage.signIn();
        RegisterPage registerPage = signInPage.register();
        registerPage.register();
    }

    @AfterMethod(alwaysRun=true)
    public void tearDown() {
        driver.quit();
    }
}

Here is my testng.xml:

<suite name="Automation Suite" parallel="methods" verbose="10">

    <test name="AccountTest">
        <classes>
            <class name="com.selenium.test.AccountTest"/>
        </classes>
    </test>

</suite>

I'm not using Grid at the moment. Trying to make this work on my local, but I will start using Saucelabs to run parallel tests moving forward.

Do you have any idea what's wrong?

Thanks in advance

Was it helpful?

Solution

TestNG will only instantiate your class once. Thus, every time your @BeforeMethod method runs, the driver field gets overwritten.

To change this, you can either run your classes in parallel, or you can set up a @DataProvider to pass in the driver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top