I've written python selenium script on Django framework. Now I have no clue how can I run the test case using browser. I can execute the test case through terminal. Now I need to execute the same test case through browser. Like with URL I need to start executing the test case.
You can test with pytest-django and selenium in Django.
First, pytest-django
and selenium
as shown below:
pip install pytest-django && pip install selenium
Then, create pytest.ini and conftest.py, test_1.py
and __init__.py
(Empty file) in tests
folder as shown below. *pytest.ini
should be put in the root directory of django-project
folder and my answer explains that multiple conftest.py
can be used:
django-project
|-core
| └-settings.py
|-my_app1
|-my_app2
|-pytest.ini
└-tests
|-__init__.py
|-conftest.py
└-test_1.py
Then, set the code below to pytest.ini
. *I recommend to set tests
to testpaths because it makes the test faster according to the doc:
# "pytest.ini"
[pytest]
DJANGO_SETTINGS_MODULE = core.settings
testpaths = tests
Now for example, set the code below to tests/conftest.py
and tests/test_1.py
to test Django Admin with the multiple headless browsers Google Chrome, Microsoft Edge and Firefox together with Selenium. *My answer explains how to test Django Admin with the multiple browsers Google Chrome, Microsoft Edge and Firefox together with Selenium:
# "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":
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
web_driver = webdriver.Chrome(options=options)
if request.param == "edge":
options = webdriver.EdgeOptions()
options.add_argument("--headless=new")
web_driver = webdriver.Edge(options=options)
if request.param == "firefox":
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
web_driver = webdriver.Firefox(options=options)
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
Finally, run the command below:
pytest
© 2022 - 2024 — McMap. All rights reserved.