You can run headless Firefox with Selenium in Python as shown below:
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument("-headless") # Here
driver = webdriver.Firefox(options=options)
Or:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("-headless") # Here
driver = webdriver.Firefox(options=options)
In addition, the examples below can test Django Admin with headless Firefox, Selenium, pytest-django and Django. *My answer explains how to test Django Admin with multiple headless browsers(Chrome, Microsoft Edge and Firefox), Selenium, pytest-django and Django:
# "tests/test_1.py"
import pytest
from selenium import webdriver
from django.test import LiveServerTestCase
@pytest.fixture(scope="class")
def firefox_driver_init(request):
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
firefox_driver = webdriver.Firefox(options=options)
request.cls.driver = firefox_driver
yield
firefox_driver.close()
@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(LiveServerTestCase):
def test_open_url(self):
self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
assert "Log in | Django site admin" in self.driver.title
Or:
# "tests/conftest.py"
import pytest
from selenium import webdriver
@pytest.fixture(scope="class")
def firefox_driver_init(request):
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
firefox_driver = webdriver.Firefox(options=options)
request.cls.driver = firefox_driver
yield
firefox_driver.close()
# "tests/test_1.py"
import pytest
from django.test import LiveServerTestCase
@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(LiveServerTestCase):
def test_open_url(self):
self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
assert "Log in | Django site admin" in self.driver.title