Run LiveServerTestCase from Docker Selenium with Django 1.11
I found the solution by overring the StaticLiveServerTestCase
and by changing the host
property.
Example:
import socketfrom django.contrib.staticfiles.testing import StaticLiveServerTestCaseclass SeleniumTestCase(StaticLiveServerTestCase): @classmethod def setUpClass(cls): cls.host = socket.gethostbyname(socket.gethostname()) super(SeleniumTestCase, cls).setUpClass()
With this solution the IP of my machine is given to the setUpClass of the LiverServerTestCase
because the default value is localhost
.
So now my liveserver is reachable outside my localhost, by using the IP..
With a bit of help from this thread and VivienCormier, this is what is working for me with Django 1.11 and docker-compose
version: '2'services: db: restart: "no" image: postgres:9.6 ports: - "5432:5432" volumes: - ./postgres-data:/var/lib/postgresql/data env_file: - .db_env web: build: ./myproject command: python manage.py runserver 0.0.0.0:8000 ports: - "8000:8000" volumes: - ./myproject:/usr/src/app depends_on: - db - selenium env_file: .web_env selenium: image: selenium/standalone-firefox expose: - "4444"
import osfrom django.contrib.staticfiles.testing import StaticLiveServerTestCasefrom selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesclass TestHomePageView(StaticLiveServerTestCase): @classmethod def setUpClass(cls): cls.host = 'web' cls.selenium = webdriver.Remote( command_executor=os.environ['SELENIUM_HOST'], desired_capabilities=DesiredCapabilities.FIREFOX, ) super(TestHomePageView, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() super(TestHomePageView, cls).tearDownClass() def test_root_url_resolves_to_home_page_view(self): response = self.client.get('/') self.assertEqual(response.resolver_match.func.__name__, LoginView.as_view().__name__) def test_page_title(self): self.selenium.get('%s' % self.live_server_url) page_title = self.selenium.find_element_by_tag_name('title').text self.assertEqual('MyProject', page_title)
.web_env file
SELENIUM_HOST=http://selenium:4444/wd/hub
Using the StaticLiveServerTestCase
doesn't seem to be necessary: the same approach works with its parent class:
class End2End(LiveServerTestCase): host = '0.0.0.0' # or socket.gethostbyname(...) like @VivienCormier did