-1

How do you determine why Firefox is crashing when run from inside some Django unittests via Selenium?

My testcase is:

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

When I run this on a headless server with:

python manage.py test functional_tests --nomigrations --failfast

it almost immediately errors with:

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

So webdriver is having trouble initializing a Firefox instance. However, the log file only shows:

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"

My versions are:

Ubuntu Version is 16.04
Selenium Version is 3.8.1
Geckodriver Version is 0.19.0
Firefox Version is 58.0.1

I tried upgrading to the most recent Selenium 3.12, Geckodriver 0.20.1, and Firefox 60, but I get the exact same error.

I also tried running all system package upgrades, and rebooting, but that had no effect.

How do I fix this, or at least get a better error message about why Firefox is crashing?

Edit: This is not the same as this question, which returns a very different error message. It's solution does not resolve my error.

Cerin
  • 60,957
  • 96
  • 316
  • 522

1 Answers1

1

First, maybe you should enable trace log by:

capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities["moz:firefoxOptions"] = {
    "log": {
        "level": "trace",
    },
}
driver = webdriver.Firefox(capabilities=capabilities)

Or

opts = Options()
opts.log.level = "trace"
driver = webdriver.Firefox(firefox_options=opts)

Second, as far as I think, this issue is usually casued by Graphic environment requirement. It seems if you are not running a firefox instance in a session with valid DISPLAY, it will exit with code 1.

So maybe your question can be resolved by:

options = Options()
profile = webdriver.FirefoxProfile()
options.add_argument("--headless")
driver = webdriver.Firefox(profile, firefox_options=options)

Finally, I must say I'm actually not sure about your problem as there is just limited information.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • That `--headless` option did the trick. I was using `xvfb` so I'm not sure why it was crashing without that, but now it's working. Thanks! – Cerin Jun 11 '18 at 22:17
  • While using `xvfb`, you still need manually set `DISPLAY` environment variable when launching your script. Maybe because of this? – Sraw Jun 12 '18 at 01:51
  • I know. That's what `pyvirtualdisplay` in my code should be doing. This setup had been working for me for months, and then something changed after an update. – Cerin Jun 12 '18 at 12:59