Getting URLError Exception when caching webdriver instances
Question: I am attempting to cache webdriver instances across test case classes. I do
not need a "clean" webdriver since I am simply using PhantomJS to query the
DOM (I do need JavaScript enabled, which is why I am not simply fetching the
source and parsing that).

The cache is a dictionary with the URL as a key and the driver instance as
value. The cache is in the base test case, and I call get() which is a method
on the base test case. This method instantiates webdriver, and goes to the url
if the driver is not in the cache already.

It appears there's some kind of socket issue when trying to access driver
properties on the cached instance in the second test case (derivedb.py). I'd
appreciate if someone could tell how to get this work.

I am getting the following output:

    
    
    $ python launcher.py
    test_a (deriveda.DerivedTestCaseA) ... Instantiate new driver
    Title is: Google
    ok
    test_b (deriveda.DerivedTestCaseA) ... Retrieve driver from cache
    Title is: Google
    ok
    test_a (derivedb.DerivedTestCaseB) ... Retrieve driver from cache
    ERROR
    
    ======================================================================
    ERROR: test_a (derivedb.DerivedTestCaseB)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/cohenaa/PycharmProjects/sanity/derivedb.py", line 7, in test_a
        print "Title is: %s" % self.driver.title
      File "/Users/cohenaa/sanity-env/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 185, in title
        resp = self.execute(Command.GET_TITLE)
      File "/Users/cohenaa/sanity-env/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 162, in execute
        response = self.command_executor.execute(driver_command, params)
      File "/Users/cohenaa/sanity-env/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
        return self._request(url, method=command_info[0], data=data)
      File "/Users/cohenaa/sanity-env/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 410, in _request
        resp = opener.open(request)
      File "/sw/lib/python2.7/urllib2.py", line 404, in open
        response = self._open(req, data)
      File "/sw/lib/python2.7/urllib2.py", line 422, in _open
        '_open', req)
      File "/sw/lib/python2.7/urllib2.py", line 382, in _call_chain
        result = func(*args)
      File "/sw/lib/python2.7/urllib2.py", line 1214, in http_open
        return self.do_open(httplib.HTTPConnection, req)
      File "/sw/lib/python2.7/urllib2.py", line 1184, in do_open
        raise URLError(err)
    URLError: <urlopen error [Errno 61] Connection refused>
    
    ----------------------------------------------------------------------
    Ran 3 tests in 1.271s
    
    FAILED (errors=1)
    

**launcher.py**

    
    
    import unittest
    from deriveda import DerivedTestCaseA
    from derivedb import DerivedTestCaseB
    
    suite = unittest.TestSuite()
    testclasses = [DerivedTestCaseA, DerivedTestCaseB]
    testloader = unittest.TestLoader()
    classes_to_names = {}
    for tc in testclasses:
        classes_to_names[tc] = testloader.getTestCaseNames(tc)
    
    for tc in classes_to_names:
        for testname in classes_to_names[tc]:
            suite.addTest(tc(testname))
    unittest.TextTestRunner(verbosity=10).run(suite)
    

**deriveda.py**

    
    
    from basetestcase import BaseTestCase
    from unittest import main
    
    class DerivedTestCaseA(BaseTestCase):
    
        def test_a(self):
            self.get("http://www.google.com")
            print "Title is: %s" % self.driver.title
        def test_b(self):
            self.get("http://www.google.com")
            print "Title is: %s" % self.driver.title
    

**derivedb.py**

    
    
    from basetestcase import BaseTestCase
    
    class DerivedTestCaseB(BaseTestCase):
    
        def test_a(self):
            self.get("http://www.google.com")
            print "Title is: %s" % self.driver.title
    

**basetestcase.py:**

    
    
    import unittest
    from selenium import webdriver
    
    class BaseTestCase(unittest.TestCase):
        cache = {}
        def get(self, url):
            if url not in self.cache:
                print "Instantiate new driver"
                self.driver = webdriver.PhantomJS() 
                self.driver.get(url)
                self.cache[url] = self.driver
            else:
                print "Retrieve driver from cache"
                self.driver = self.cache[url]
    
        @classmethod
        def tearDownClass(cls):
            for url in BaseTestCase.cache:
                BaseTestCase.cache[url].quit()
    


Answer: Ahhh. I see now. The driver is quitting after each test case. If I quit after
the suite is run instead, no error.

