jdroetti
jdroetti

Reputation: 45

Is it possible to write a unittest script that does not close the browser on each test?

I am running on windows 10 with python 3.6.2 and selenium 3.11. I currently have a small unit test which works but on each new test it opens up a new browser. The first test is to login but when I move to the next test it starts a new browser therefore I cannot continue my unit testing. Below is my current code

class LoginTest(unittest.TestCase):
logging.basicConfig(filename='test.log', level=logging.INFO,
                    format='%(asctime)s:%(levelname)s:%(lineno)d:%(message)s')

def setUp(self):
    logging.info('starting browser')
    self.driver = DriverFactory().run_browser()
    base_page = Page(self.driver)
    base_page.open("url")
    page_title = self.driver.title
    assert page_title == "title"
    logging.info('Login page title: ' + page_title)

def test_login(self):
    logging.info('Logging in')
    login_page = LoginPage(self.driver)
    login_page.username().send_keys("username")
    login_page.password().send_keys("password")
    login_page.sign_in().click()
    page_title = self.driver.title
    Page(self.driver).wait()
    assert page_title == "title"
    logging.info('Main page title: ' + page_title)

def test_search(self):
    logging.info("Search")
    main_page = MainMenu(self.driver)
    tab = main_page.Loans(self.driver)
    main_page.tab_search('Foo')
    main_page.switchTo_left_frame()
    tab.search().send_keys('bar')
    tab.top_result()
    page_title = self.driver.title
    logging.info("Loan Page Title: " + page_title)

def tearDown(self):
    self.driver.quit()

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

This script will login then close that browser and start a new one and try to complete the search test which fails. I am unable to use url navigation due to the security of the webpage. Any help would be greatly appreciated thanks.

Upvotes: 1

Views: 410

Answers (1)

jfleach
jfleach

Reputation: 513

Instead of using setUp you could use setUpClass which will only be ran once in the unit test framework.

Upvotes: 2

Related Questions