I installed pytest-django and selenium in Django as shown below.
pip install pytest-django && pip install selenium
Then, I created pytest.ini, test_1.py
and __init__.py
(Empty file) in tests
folder as shown below:
django-project
|-core
| └-settings.py
|-my_app1
|-my_app2
|-pytest.ini
└-tests
|-__init__.py
└-test_1.py
Then, I set the code below to pytest.ini
:
# "pytest.ini"
[pytest]
DJANGO_SETTINGS_MODULE = core.settings
testpaths = tests
Finally, I could test Django (Admin) with the multiple browsers Google Chrome, Microsoft Edge and Firefox separately but not together with Selenium as shown below so the code is a lot:
# "tests/test_1.py"
import pytest
from selenium import webdriver
from django.test import LiveServerTestCase
""" Google Chrome Test Begin """
@pytest.fixture(scope="class")
def chrome_driver_init(request):
chrome_driver = webdriver.Chrome()
request.cls.driver = chrome_driver
yield
chrome_driver.close()
@pytest.mark.usefixtures("chrome_driver_init")
class Test_URL_Chrome(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
""" Google Chrome Test End """
""" Microsoft Edge Test Begin """
@pytest.fixture(scope="class")
def edge_driver_init(request):
edge_driver = webdriver.Edge()
request.cls.driver = edge_driver
yield
edge_driver.close()
@pytest.mark.usefixtures("edge_driver_init")
class Test_URL_Edge(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
""" Microsoft Edge Test End """
""" Firefox Test Begin """
@pytest.fixture(scope="class")
def firefox_driver_init(request):
firefox_driver = webdriver.Firefox()
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
""" Firefox Test End """
So, how can I test Django with multiple broswers together with Selenium to reduce the code?