0

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)
1
  • To use the same session you need to add "set up"-method with the ability to login via cookies or Local Storage (it depends on the realization it in your app). Commented Jul 10, 2018 at 12:10

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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")

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.