当我通过python manage.py test
运行它时,我有一个单元测试通过,但是当我从PyCharm中运行它时失败了。
def test_alpha(self):
from selenium.webdriver.common.utils import free_port
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=free_port())
driver.quit()
从PyCharm运行时遇到的异常是
WebDriverException: Message: 'Can not connect to GhostDriver'
我花了相当多的时间来研究这个问题,我注意到当我手动指定端口时,测试在PyCharm中传递。
# suppose 50000 happens to be a free port on your computer
driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=50000)
快速回顾一下:
python manage.py test
port=free_port()
,测试在PyCharm中失败PyCharm做的是让测试无法连接到Ghostdriver?
# For convenience, the `free_port()` code snippet is here
# selenium.webdriver.common.utils.freeport
def free_port():
free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
free_socket.bind(('127.0.0.1', 0))
free_socket.listen(5)
port = free_socket.getsockname()[1]
free_socket.close()
return port
尝试使用npm:https://www.npmjs.com/package/phantomjs安装phantomjs。我使用Mac OS X,我有一个类似的问题使用selenium和django。