我使用以下方法创建了许多测试用例:
大约50个测试用例使用了这个场景
try:
caps = DesiredCapabilities.FIREFOX
caps["wires"] = True
driver = webdriver.Firefox(capabilities=caps)
driver.maximize_window()
self.log("Selenium driver created for %s" % browser)
return self.STATUS.PASS, driver
except Exception, e:
self.log("Error when creating selenium driver")
self.log(traceback.format_exc())
return self.STATUS.FAIL
这段代码很简单,创建了一个Firefox Web驱动程序,它在所有测试用例中运行良好但是1,特别是当我在日常执行中执行它时,我得到了以下错误
Traceback (most recent call last):
File "C:\QA Tools\selenium\CreateSeleniumDriver.py", line 26, in run
driver = webdriver.Firefox(capabilities=caps)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 154, in __init__
keep_alive=True)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 140, in __init__
self.start_session(desired_capabilities, browser_profile)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 295, in execute
response = self.command_executor.execute(driver_command, params)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 464, in execute
return self._request(command_info[0], url, body=data)
File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 538, in _request
body = data.decode('utf-8').replace('\x00', '').strip()
File "c:\python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
AttributeError: 'NoneType' object has no attribute 'utf_8_decode'
Selenium driver created for Firefox
Error when creating selenium driver
Traceback (most recent call last):
File "C:\QA Tools\selenium\CreateSeleniumDriver.py", line 63, in run
return self.STATUS.PASS, driver
UnboundLocalError: local variable 'driver' referenced before assignment
所以,这打破了selenium web驱动程序的一部分,但我不明白为什么!
任何可能出错的想法?
答案 0 :(得分:0)
看起来你正在遇到官方Python bug追踪器上提到的这个错误:https://bugs.python.org/issue14847
那里的评论表明这个问题可能与以下两个方面有关:
安装包含distribute
的软件包,因为已知错误至少存在于distribute 0.6.26
中。如果您每天执行"依赖于使用此distribute
的错误版本进行安装,这可能会解释您的问题。你知道吗?您是否可以更新环境中使用的distribute
版本,或者使用虚拟环境来避免它?
其他一些操纵sys.modules
的方式,导致找不到codecs
,而是提供None
。您的代码是否在某种奇特的测试设置中执行此类操作?
根据跟踪判断,为了简洁起见,您修改了代码,但这种方式可能无法完全捕获原始逻辑。 UnboundLocalError
表示您的driver
作业已被跳过,而且您没有立即回复:
try:
caps = DesiredCapabilities.FIREFOX
caps["wires"] = True
# This line raises an AttributeError deep inside a standard python lib:
driver = webdriver.Firefox(capabilities=caps)
driver.maximize_window()
self.log("Selenium driver created for %s" % browser)
except Exception, e:
# The driver variable was never actually created or assigned due to the
# AttributeError above.
self.log("Error when creating selenium driver")
self.log(traceback.format_exc())
# Some omitted code...
# The driver variable does not exist past the try if the exception happened
# before assignment to driver. So this raises the UnboundLocalError:
return self.STATUS.PASS, driver
您可以通过在except中进行返回,或者将UnboundLocalError
预分配到driver
或其他内容来轻松修复None
,但这不会解决根本问题创建驱动程序。这听起来像是执行环境的问题。您的运行基础架构中的某些东西可能正在做一些花哨的东西(您使用什么作为您的测试运行器?)或者在您的日常执行运行程序的Python环境中可能存在某些损坏的东西。根据问题的原因,您可以通过为所有测试执行定义特定的virtualenv
来解决问题,因此您可以确保它与自动每日执行中的手动运行相同。