我在使用 selenium 和 PhantomJS 驱动程序运行单元测试时遇到问题。它似乎与 PhantomJS 进程中 stderr/stdout 的资源争用有关。错误是:
$ python -m unittest selenium_failure.SeleniumTestCase
[]
[{u'timestamp': 1395857498698, u'message': u'{"log":{"version":"1.2","creator":{"name":"PhantomJS","version":"1.9. 7"},"pages":[{"startedDateTime":"2014-03-26T18:11:38.347Z","id":"https://www.google.com/","title":"Google ","pageTimings":{"onLoad":294}}],"entries":[{"startedDateTime":"2014-03-26T18:11:38.344Z","time":127,"request":{ "方法":"GET","url":"https://www.google.com/","httpVersion":"HTTP/1.1","cookies":[],"headers":[{"名称":"User-Agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"},{"name":"接受","值":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"queryString":[],"headersSize":-1 ,"bodySize":-1},"response":{"status":200,"statusText":"OK","httpVersion":"HTTP/1.1","cookies":[],"headers":[ {"name":"Date","value":"2014 年 3 月 26 日星期三 18:11:37 GMT"},{"name":"Expires","value":"-1"},{"name ":"Cache-Control","value":"private, max-age=0"},{"name":"Content-Type","value":"text/html; charset=UTF-8 "},{"name":"Set-Cookie",E
==================================================== =====================
错误:test_that_something_fails (selenium_failure.SeleniumTestCase)
-------------------------------------------------- --------------------
回溯(最近一次通话最后):
文件“selenium_failure.py”,第 16 行,在 test_that_something_fails
打印 repr(self.selenium.get_log('har'))
IOError:[Errno 35] 资源暂时不可用
-------------------------------------------------- --------------------
在 2.398 秒内运行 1 次测试
失败(错误=1)
更详细的要点在这里:https://gist.github.com/lucaswiman/9788422
import unittest
import logging
from selenium.webdriver import phantomjs
import sys
class SeleniumTestCase(unittest.TestCase):
def setUp(self):
self.selenium = phantomjs.webdriver.WebDriver()
def tearDown(self):
self.selenium.quit()
def test_that_something_fails(self):
self.selenium.get('https://www.google.com')
print repr(self.selenium.get_log('browser'))
print repr(self.selenium.get_log('har'))
raise AssertionError()
该错误在 OS X 上可以重现,但在 Ubuntu 12.04 上不行。我相信它并不特定于 OS X,因为我在 Ubuntu 上运行的集成测试中看到了类似的错误,尽管我无法以孤立的形式重现它。
- Python 2.7.6
- 硒==2.35.0
- phantomjs@1.9.7