在缓存webdriver实例时获取URLError异常

时间:2013-12-26 19:34:05

标签: selenium selenium-webdriver

我正在尝试跨测试用例类缓存webdriver实例。我不需要一个“干净”的webdriver,因为我只是使用PhantomJS来查询DOM(我确实需要启用JavaScript,这就是为什么我不是简单地获取源并解析它)。

缓存是一个字典,其中URL作为键,驱动程序实例作为值。缓存在基本测试用例中,我调用get()这是基本测试用例的方法。此方法实例化webdriver,如果驱动程序不在缓存中,则转到url。

在第二个测试用例(derivedb.py)中尝试访问缓存实例上的驱动程序属性时,似乎存在某种套接字问题。如果有人能说出如何开展这项工作,我将不胜感激。

我得到以下输出:

$ 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()

1 个答案:

答案 0 :(得分:0)

唉唉。我现在明白了。驱动程序在每个测试用例后退出。如果我在运行套件后退出,则没有错误。