Is there a way to prevent setUp() from launching a browser for each test method in my test case in python?

I’m practicing writing test cases for web automation and I have written functions to test login, find my username in the user home page and test logout functionality of GitHub. However, I’ve learned through both experience and reading that setUp() is initiated before each test method, and my problem is that before every test method it opens a new browser. I want all my test methods to continue testing on the same browser in the same session.

Here is my code to show you what I’ve tried:

import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import element_to_be_clickable

class GitHubLoginTest(unittest.TestCase):
    initialized = 0
    completed = 0

    def setUp(self):
        if self.initialized < 1:
            self.initialized = 1
            chrome_options = Options()
            chrome_options.add_experimental_option("detach", True)
            self.driver = webdriver.Chrome(options=chrome_options)
        else:
            pass

    def test_login(self):
        driver = self.driver
        driver.get("https://github.com")
        driver.find_element(By.LINK_TEXT, "Sign in").click()
        username_box = WebDriverWait(driver, 10).until(element_to_be_clickable((By.ID, "login_field")))
        username_box.send_keys("[email protected]")
        password_box = driver.find_element(By.NAME, "password")
        password_box.send_keys("onefaithmanymembers")
        password_box.submit()
        self.completed += 1
        print(self.completed)
        print(self.initialized)

    def test_username_presence(self):
        print(self.completed)
        print(self.initialized)
        self.assertIn("SubjectofthePotentate", self.driver.page_source)
        self.driver.find_element(By.CLASS_NAME, "AppHeader-user").click()
        profile_label = self.driver.find_element(By.CLASS_NAME, "lh-condensed")
        user_label = profile_label.get_attribute("innerHTML")
        print(user_label)
        self.assertIn("SubjectofthePotentate", user_label)
        self.completed += 1
            print(self.completed)

    def test_logout(self):
        self.driver.find_element(By.CLASS_NAME, "DialogOverflowWrapper")
        self.driver.find_element(By.ID, ":r11:").click()
        sign_out = WebDriverWait(self.driver, 10).until(element_to_be_clickable((By.NAME, "commit")))
        sign_out.click()
        self.completed += 1
        print(self.completed)

    def tearDown(self):
        if self.completed == 3:
            self.driver.close()
        else:
            pass

if __name__ == "__main__":
    unittest.main()

I tried creating attributes which I called initialization and completed to prevent setUp() from loading another browser and also to prevent tearDown() from closing the browser before all the tests are finished, but a new browser is opened three times, one for each test function.

I noticed that when print(self.completed) and print(self.initialized) are executed in the test_login(self) method that they both equal 1, but when print(self.completed) and print(self.initialized) are executed again in the test method test_username_presence(self), self.completed is equal to 0 and self.initialized is equal to 1, so I think that means that the setUp(self) method is being executed before each test method and the attributes I defined at the class level are being reset for some reason. I’ve tried initializing these attributes using setUpClass(cls) and I also tried this:

def __init__(self, other):
    super().__init__(other)
    self.completed = 0
    self.initialized = 0

But I got the same results, multiple browsers and the last two are empty webpages with no URL. Anyone know how I can continue the tests on one browser in one session?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật