Selenium Python run multiple test cases how do i log in once and use the same instance browser

0
0

I have created some Test Cases in Selenium Python and I have put it in a Test Suite. Each time a Test Case runs it opens the browser and navigates to the URL.
It then logs in, does some tests and then logs out and the browser closes.

Is there a way to run the tests to only open 1 instance of the browser, log in once and keep using that instance for the rest of the test cases?

I do not want to close the browser for every single test case and open a new browser and log in each time.

For e.g.
Test Case 1 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

Test Case 2 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

Test Case 3 opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

and so on.

I would like to do it this way.

Test Case 1 runs, opens the browser, navigate to URL, log in, run some tests. Log out and close the browser.

Test Case 2 use the same browser from test case 1, you are still logged in, run some tests.

Test Case 3 use the same browser from test case 1, you are still logged in, run some tests.

The last test case use the same browser from test case 1, you are still logged in, run some tests. Log out, close the browser.

Opening a new browser for every single test case and logging in takes longer for the tests to complete.

My code snippet is as follows:

class BaseTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls
.driver = webdriver.Ie(Globals.IEdriver_path)
cls
.driver.get(Globals.URL_justin_pc)
cls
.login_page = login.LoginPage(cls.driver)
cls
.driver.implicitly_wait(120)
cls
.driver.maximize_window()
@classmethod
def tearDownClass(cls):
cls
.login_page.click_logout()
cls
.driver.close()
Test Case 1
class AdministrationPage_TestCase(BaseTestCase):
def test_add_Project(self):
print "*** test_add_project ***"
self
.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
menu_bar
= MenuBarPage(self.driver)
administration_page
= menu_bar.select_menuBar_item("Administration")
administration_page
.click_add_project_button()
administration_page
.add_project(project_name, Globals.project_description)
administration_page
.click_save_add_project()
# etc ...
def test_edit_Project(self):
...
Test Case 2
class DataObjectsPage_TestCase(BaseTestCase):
def testa_add_Data_Objects_Name(self):
print "*** test_add_Data_Objects - Name ***"
self
.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
menu_bar
= MenuBarPage(self.driver)
data_configuration_page
= menu_bar.select_menuBar_item("Data Configuration")
project_navigator
= ProjectNavigatorPage(self.driver)
data_objects
= project_navigator.select_projectNavigator_item("Data Objects")
data_objects
.click_add_button_for_data_objects()
def testb_add_Data_Objects_Address(self):
print "*** test_add_Data_Objects - Address ***"
...
def testc_add_Data_Objects_Phone(self):
...
Test Case 3 and so on

My Test Suite is:

def suite():
test_suite
= unittest.TestSuite()
test_suite
.addTest(unittest.makeSuite(TestCases.AdministrationPage_TestCase.AdministrationPage_TestCase))
test_suite
.addTest(unittest.makeSuite(TestCases.DataObjectsPage_TestCase.DataObjectsPage_TestCase))
# etc...

Thanks,

Riaz

  • You must to post comments
0
0

Slow automated tests caused by needlessly opening and closing browsers, hardcoded delays etc. are, as you say, a huge waste of time, and almost always avoidable in my experience.

The good news is that you don’t need to do anything special to avoid this. If your tests are independent and run in sequence, also assuming that you only have one browser/version and set of Capabilities, then all your test runner needs to do is:

  • create singleton Driver at start of run (or application, if multiple runs allowed; or lazily when first required)
  • run each test in sequence, with no explicit close or quit calls, but with all other suitable cleanup (clearing any created cookies, logging out, clearing sessions, Local storage etc.)
  • quit Driver at the (very) end

It’s not very much more work to support multiple browsers / capabilities in much the same way.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website