如何通过Selenium在某些Django单元测试中运行时确定Firefox崩溃的原因?
我的测试用例是:
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from pyvirtualdisplay import Display
from selenium import webdriver
class Tests(StaticLiveServerTestCase):
def setUp(self):
super(Tests, self).setUp()
self.vdisplay = Display(visible=0, size=(1920, 1080), backend='xvfb')
self.vdisplay.start()
profile = webdriver.FirefoxProfile()
log_path = '/tmp/tests.log'
self.driver = webdriver.Firefox(profile, log_path=log_path)
def test_abc(self):
blah
当我在无头服务器上运行时:
python manage.py test functional_tests --nomigrations --failfast
它几乎立即出错:
ERROR: test_abc (myproject.functional_tests.tests.Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/src/myproject/functional_tests/tests.py", line 15, in setUp
self.driver = webdriver.Firefox(profile, log_path=log_path)
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__
keep_alive=True)
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/myproject/src/buildbot/worker3/myproject_runtests/build/.env/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Process unexpectedly closed with status: 1
因此webdriver
无法初始化Firefox实例。但是,日志文件仅显示:
1528498122788 geckodriver INFO geckodriver 0.19.0
1528498122799 geckodriver INFO Listening on 127.0.0.1:39255
1528498123950 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.LrTWF7H6fk2y"
我的版本是:
Ubuntu Version is 16.04
Selenium Version is 3.8.1
Geckodriver Version is 0.19.0
Firefox Version is 58.0.1
我尝试升级到最新的Selenium 3.12,Geckodriver 0.20.1和Firefox 60,但我得到了完全相同的错误。
我还尝试运行所有系统软件包升级,然后重新启动,但这没有效果。
如何解决此问题,或至少获得有关Firefox崩溃原因的更好错误消息?
编辑:这与this question不同,它返回一个非常不同的错误消息。它的解决方案无法解决我的错误。
答案 0 :(得分:1)
首先,也许您应该通过以下方式启用trace log
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities["moz:firefoxOptions"] = {
"log": {
"level": "trace",
},
}
driver = webdriver.Firefox(capabilities=capabilities)
或者
opts = Options()
opts.log.level = "trace"
driver = webdriver.Firefox(firefox_options=opts)
其次,据我所知,这个问题通常由图形环境要求引起。看来如果你没有在有效DISPLAY
的会话中运行firefox实例,它将以代码1
退出。
所以也许你的问题可以解决:
options = Options()
profile = webdriver.FirefoxProfile()
options.add_argument("--headless")
driver = webdriver.Firefox(profile, firefox_options=options)
最后,我必须说我实际上对您的问题不确定,因为信息有限。