How to run my python selenium scripts using browser?
Asked Answered
S

1

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.

Shamrock answered 19/10, 2019 at 8:31 Comment(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?Indistinct
Yes, I mean tests to start when I access a browser or when I click a button a button on that URL webpageShamrock
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/flaskIndistinct
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?Shamrock
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).Mccarthy
Also note: you may need to discern your tests between functional (i.e., how does the interface work) and unit (i.e., is each method/route/model working as expected).Mccarthy
C
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
Chas answered 2/9, 2023 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.