How to share same browser session between two methods (TestNG)?



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



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



Suggested Topics

  • 2
  • 2
  • 4
  • 2
  • 2
  • 2
  • 2
  • 2