2

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.

6
  • Do you mean you need the tests to start when you access a certain URL? or do you want the tests to actually open a browser window and show you what the tests are doing? Commented Oct 19, 2019 at 9:11
  • Yes, I mean tests to start when I access a browser or when I click a button a button on that URL webpage Commented Oct 19, 2019 at 12:40
  • You need to run a webserver and serve that webpage with the button you want to click. Check a web framework like Django or Flask and start from there. djangoproject.com/start or palletsprojects.com/p/flask Commented Oct 19, 2019 at 12:45
  • Thank you devbob for your reply and time. But I have already build an project name blog in that I have users app and in that test.py file resides. Now how I can point to the file on web brower to start? Commented Oct 19, 2019 at 12:55
  • I suggest this resource: Obey the Testing Goat. This looks like a fit in your requirements (i.e., run a script so it shows a webpage and test your Django app functionally). Commented Oct 21, 2019 at 5:14

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.