1

I am writing TestNG code for the below scenario and do not know how to share browser session between two methods:
1. Creating a data driven testNG suite to login using 10 different usernames
I need to write two methods and both methods should share the same browser session. Any idea how to share browser session between two test methods using TestNG?

1 Answer 1

2

I don't quite get why you would like to share browser session. UI test methods should be run in the own independent browser. In case you insist, you could use @BeforeClass of TestNG and have driver set up done in @BeforeClass method. Now both of your methods should be either in this class or class with extends the set up class. Confused? This might help -

public class TestCase {

  @BeforeClass
  public void setup() {
    // Set up driver here

  @BeforeClass
  public void tearDown() {
    // kill driver here
  }

}

And you test class here -

public class MyHorribleClassWhichUsesSameBrowserForMultipleTests extends TestCase{

  @Test
  public void method1() {
    // test code here

  @Test
  public void method2() {
    // test code here
  }
}

Here both test methods would be using same browser one after another.

1
  • Is there not a parameter in @WebTest like "keepBrowserOpen" or "OpenNewSession" (bool) ?
    – R11G
    Oct 12, 2013 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.