How to test Django with multiple broswers together with Selenium to reduce the code?
Asked Answered
T

1

0

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?

Trainbearer answered 5/9, 2023 at 2:12 Comment(0)
T
0

You can test Django (Admin) with the multiple browsers Google Chrome, Microsoft Edge and Firefox together with Selenium to reduce the code as shown below. *My answer explains how to test Django (Admin) with the multiple headless browsers Google Chrome, Microsoft Edge and Firefox together with Selenium to reduce the code as shown below:

# "tests/test_1.py"

import pytest
from selenium import webdriver

@pytest.fixture(params=["chrome", "edge", "firefox"], scope="class")
def browser_driver_init(request):
    if request.param == "chrome":
        web_driver = webdriver.Chrome()
    if request.param == "edge":
        web_driver = webdriver.Edge()
    if request.param == "firefox":
        web_driver = webdriver.Firefox()
    request.cls.driver = web_driver
    yield
    web_driver.close()

@pytest.mark.usefixtures("browser_driver_init")
class Test_URL_Browser:
    def test_open_url(self, live_server):
        self.driver.get(("%s%s" % (live_server.url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title

And, you can also use conftest.py as shown below:

django-project
 |-core
 |  └-settings.py
 |-my_app1
 |-my_app2
 |-pytest.ini
 └-tests
    |-__init__.py
    |-conftest.py # Here
    └-test.py
# "tests/conftest.py"

import pytest
from selenium import webdriver

@pytest.fixture(params=["chrome", "edge", "firefox"], scope="class")
def browser_driver_init(request):
    if request.param == "chrome":
        web_driver = webdriver.Chrome()
    if request.param == "edge":
        web_driver = webdriver.Edge(options=options)
    if request.param == "firefox":
        web_driver = webdriver.Firefox()
    request.cls.driver = web_driver
    yield
    web_driver.close()
# "tests/test_1.py"

import pytest

@pytest.mark.usefixtures("browser_driver_init")
class Test_URL_Browser:
    def test_open_url(self, live_server):
        self.driver.get(("%s%s" % (live_server.url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title
Trainbearer answered 5/9, 2023 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.