I'm using Selenium to run some integration tests for a Django app (using postgres
for the development database).
I set-up the tests by creating a Model object. The tests then interact with the object through the browser (selenium-webdriver
), or directly through database queries.
The tests that use webdriver
cannot access the Model object. The database queries can.
I've come to the conclusion that webdriver
is not being pointed at the correct database.
I've tried using LiveServerTestCase
both explicitly giving it the port and URL I want to use (localhost:8001
) and using live_server_url
to allow it to decide for itself. Neither have worked.
Here's the form being tested - it's a simple select from a list of objects.
forms.py
class FooForm(form.Form):
foo = forms.ModelChoice.Field(
required=False,
label='select',
queryset=Foo.objects.all(),
widget=forms.Select(),
)
Here's the DB settings.
settings.py
DATABASES = {
'default':{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'app',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Here are the tests.
test.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.select import Select
from django.test import LiveServerTestCase
from app.models import Foo
class BasicTestCase(LiveServerTestCase):
def setUp(self):
foo = Foo.objects.create(name='test_foo')
self.selenium = webdriver.Firefox()
super(BasicTestCase, self).setUp()
def tearDown(self):
self.selenium.quit()
super(BasicTestCase, self).tearDown()
def test_can_interact_via_db_query(self):
self.assertEqual(Foo.objects.get(id=1).name, 'test_foo')
def test_can_interact_via_webdriver_query(self):
self.selenium.get('%s%s' %(self.live_server_url, '/page/'))
elem = Select(self.selenium.find_element_by_id('id_foo'))
foo_name = elem.select_by_index(0)
self.assertEqual(foo_name, 'test_foo')
I'd expect both of these tests to pass.
I can see the browser being opened and the page being interacted with.
test_can_interact_via_db_query
passes.
test_can_interact_via_webdriver_query
fails with an assertion error None != test_foo
.