5

I have the following code in python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from unittestzero import Assert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import ElementNotVisibleException
import unittest, time, re

class HomePageTest(unittest.TestCase):
    expected_title="  some title here "
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://somewebsite.com"
        self.verificationErrors = []

    def test_home_page(self):
        driver=self.driver
        driver.get(self.base_url)
        print "test some things here"




    def test_whatever(self):
        print "test some more things here"

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


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

My problem is after the function test_home_page, the firefox instance closes and opens again for the next test_whatever function. How can I do his so that all the test cases are executed from the same firefox instance.

1
  • 1
    any code in your setUp/tearDown will be run for each test case executed. Commented Jun 15, 2012 at 11:21

6 Answers 6

8

Usually you want the browser to close between tests so that you start each test with a clean cache, localStorage, history database, etc. Closing the browser between tests does slow down the tests, but it saves in debug time because a test doesn't interact with the browser cache and history of a previous test.

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

1 Comment

+1. And in the long run, clean browser instances between tests will actually enable you to speed up test execution by allowing tests to run concurrently. If you have several tests that rely on the state of the browser being preserved from test to test, you are forced to run them serially. However, if each test is able to run from a clean slate, you now have the ability run them all at the same time. For a suite of 5 test cases, each taking 60 seconds to complete, your total test time is reduced from 5 minutes to 1 minute. Imagine the savings if you have 1000 test cases!
3

Initialize the firefox driver in __init__:

class HomePageTest(unittest.TestCase):
    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://somewebsite.com"
        self.verificationErrors = []

    ...

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

Comments

3

use setUpClass and tearDownClass

class HomePageTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()

    def setUp(self):
        self.base_url = "https://somewebsite.com"
        self.verificationErrors = []

    def tearDown(self):
        self.driver.get(self.base_url)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()



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

Comments

0

I think WebDriver Plus solves your problem. Here it is. http://webdriverplus.org/en/latest/browsers.html You can reuse the same browser instance across all unit tests using this .

1 Comment

The link is broken
0
def suite():  

    suite = unittest.TestSuite()  
    suite.addTest(HomePageTest("test_home_page"))  
    suite.addTest(HomePageTest("test_whatever"))  
    return suite  

if __name__ == "__main__":

    unittest.TextTestRunner().run(suite()) 

Run many testcase with the same Firefox instance. Btw, i have a question hope someone could know it and answer to me. How could i execute the same testcase in different browsers?

Comments

0

use setUpClass and tearDownClass with @classmethod decorator.

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.