0

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

Jail
  • 800
  • 2
  • 13
  • 28

1 Answers1

1

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.

Nathan Merrill
  • 6,018
  • 5
  • 32
  • 49
  • Hello Nathan, thanks for the response. I've tried creating separate classes for each method, and it worked but I might need to run several methods that are in same class in the future. I've checked AfterMethod and it doesn't take dataProvider as parameter. Can you go in more details for that annotation? Thanks – Jail Apr 07 '14 at 14:14
  • You can have multiple methods in a class, but I wouldn't recommend running them parallel. If you wanted to use the `@DataProvider`, you would have to close all of the drivers in a `@AfterClass` I believe. I'm not completely sure which methods accept data providers and which don't. – Nathan Merrill Apr 07 '14 at 14:24
  • I have found that things are much easier with TestNG and Selenium if you chunk by classes. Instantiate in your `@BeforeClass`, close in your `@AfterClass`, and parallelize with classes. If you want to use multiple drivers, then I would recommend using a factory. – Nathan Merrill Apr 07 '14 at 14:26
  • Thanks for the response. I will think about it. I feel like at some point I will need to use multiple drivers. That's why I was looking an answer for that one too. Do you have any particular recommendations for that? – Jail Apr 07 '14 at 16:02
  • You could create subclasses that share methods. Then you could pass in the driver as parameters. – Bob Dalgleish Apr 07 '14 at 16:47