Roman Azarov
Roman Azarov

Reputation: 131

Python selenium share second browser between test

I'm quite new to selenium and may be doing something wrong. Please fix me!

I'm creating some E2E tests, part of them require second account. Each time I open new browser, I have to make login procedure, that takes time. So, I decided to keep the second browser open between tests and reuse it.

But I can't pass the newly created selenium object to the second test. What I'm doing wrong?

class RunTest(unittest.TestCase): 
    @classmethod
    def setUpClass(self):
        #main browser that I always use
        self.driver = webdriver.Chrome(...)

    def init_second_driver(self):
        #second browser that could be used by some tests
        self.second_driver = webdriver.Chrome(...)

    def test_case1(self):
        if not self.second_driver:
            self.init_second_driver() 
        #some tests for case 1

    def test_case2(self):
        if not self.second_driver: #this check always fails! WHY?
            self.init_second_driver()
        #some tests for case 2


Thank you in advance

Upvotes: 0

Views: 129

Answers (1)

RichEdwards
RichEdwards

Reputation: 3743

Everytime you create your chromedriver object it's default option is to create a new Chrome profile. Think of a profile as your local cookie store and chache.

You want this to happen. Selenium is designed for testing and logging in each time without history ensures you tests always start from the same state (not logged in and no cookies).

If you have a lot of tests and want your suite to run faster consider running tests in parallel.

For now, if you want to try sharing state between tests (i.e. staying logged in) you can instruct chrome to reuse a profile with the following option/tag:

options = webdriver.ChromeOptions() 
options.add_argument('--user-data-dir=C:/Path/To/Your/Testing/User Data')
driver  = webdriver.Chrome(options=options)

That should remove the need for a second browser your state.

Upvotes: 1

Related Questions