Home > Enterprise >  StaticLiveServerTestCase with Selenium: ERR_CONNECTION_REFUSED
StaticLiveServerTestCase with Selenium: ERR_CONNECTION_REFUSED

Time:03-22

I'm running the default snippet from the docs for testing with selenium but 'm using Chrome driver. When I run the tests using python manage.py test it can't connect to the server seems it won't start, throwing the error ::ERR_CONNECTION_REFUSED. Any ideas?

Here is the snippet:

from django.test import LiveServerTestCase
from selenium import webdriver

class MySeleniumTests(LiveServerTestCase):
    # fixtures = ['user-data.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.selenium = webdriver.Chrome(executable_path='./chromedriver')
        cls.selenium.implicitly_wait(10)

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super().tearDownClass()

    def test_login(self):
        self.selenium.get('http://localhost:8000/accounts/login')

CodePudding user response:

Why is the url hard coded(localhost:8000) in test_login? Since you are using LiveServerTestCase, you would be able to access the server base URL with self.live_server_url. This is also mentioned on the docs link you have added in the description. The connection refused error is most likely happening due to the hard-coded URL for one of the following reasons:

  1. No server or service is active at 8000 port
  2. Allowed origin setting is not allowing the connection, though this is entirely dependent on the settings.
  • Related