tack
tack

Reputation: 325

django selenium fails to load localhost

I have a simple code for testing:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title

Run it: python3 functional_tests.py (or ./manage.py test functional_tests)

Firefox shows page: Title: Problem loading page Body: Unable to connect ...

If I run: "./manage.py runserver" everything is fine, I can see "django hello page" in my browser. Also if i try "browser.get('http:// microsoft. com')" it works just fine.

Same issue with Chrome and the same on Windows 7 x64 and Ubuntu 14.04 x64.

Selenium 2.47.3 Chromedriver 2.19

Any clues?

UPD.

Ok. I didn't run the server. But i had same problem with LiveServerTestCase.

from selenium import webdriver
from django.test import LiveServerTestCase

class GeneralFunctionalTests(LiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def test_can_navigate_site(self):
        self.browser.get('http://localhost:8000')
        assert 'Django' in self.browser.title

Thank You!

Upvotes: 3

Views: 2840

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

Case-1 : Accessing 'http://localhost:8000' without running Django server

In the first case, the browser is trying to access a url on the localhost. This requires a Django server to be running alongside for the browser to open the url correctly. Since you are not running a Django server alongside with it, you get the message Unable to connect ..

Case-2 : Accessing 'http://localhost:8000' while running the Django server alongside

When you run a Django server alongside it, the browser will be able to access the localhost url as there is a server running at port 8000 which will listen to request from the browser. So, you are able to see the Django Hello Page.

Case-3: Accessing 'http://microsoft.com

Same is the case with accessing Microsoft's website using selenium. There is a server running on their end which listens to requests made to http://microsoft.com due to which the page gets loaded.

What you can also do?

You can also use a LiveServerTestCase for testing.

From the docs:

it launches a live Django server in the background on setup, and shuts it down on teardown.

Upvotes: 2

Related Questions