Sabari Deepak
Sabari Deepak

Reputation: 11

Using same browser session in different cases

In the below code am trying to use the same browser session in different test cases, but after running its observed that two different browser sessions are opened for each test case. Please guide me to fix this issue:

driver1 = webdriver.Chrome(
            executable_path="C:\\Program Files (x86)\\chromedriver.exe")

class Test_Trials(unittest.TestCase):

    def test_1(self):
        driver1.set_page_load_timeout(20)
        driver1.get("http://192.168.221.238:8180/tnp/")
        driver1.maximize_window()

    def test_2(self):
        driver1.find_element_by_id("j_username").send_keys("admin")
        driver1.find_element_by_name("j_password").send_keys("admin1001")
        driver1.find_element_by_class_name("gwt-Button").click()
        driver1.set_page_load_timeout(20)

Upvotes: 0

Views: 204

Answers (2)

Arnon Axelrod
Arnon Axelrod

Reputation: 1672

Try declaring and initializing driver1 inside the class. Alternatively, only declare it inside the class (initialize with None), and initialize it in the setupClass method like this:

@classmethod
def setUpClass(cls):
    driver1 = webdriver.Chrome(
        executable_path="C:\\Program Files (x86)\\chromedriver.exe")

Upvotes: 0

AutomatedOwl
AutomatedOwl

Reputation: 1089

Try to use pytest with module setup, initilazing your driver before class execution:

class Test_Trials(unittest.TestCase):

  def setup_module(module):
      driver1 = webdriver.Chrome(
          executable_path="C:\\Program Files (x86)\\chromedriver.exe")

Or class method:

 @classmethod
 def setup_class(cls):
      driver1 = webdriver.Chrome(
          executable_path="C:\\Program Files (x86)\\chromedriver.exe")

Full documentation: https://docs.pytest.org/en/latest/xunit_setup.html

Upvotes: 1

Related Questions