Reputation: 21
I'm trying to test that a user can login and view their account dashboard with selenium.
I create the user in the setUp() function but when I try and login, the login fails. It seems the user is not being created. (although when i add a breakpoint in the setUp..it is)
class MyTestCase(TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
# create user
self.user = User.objects.create_user(username=USERNAME,
email=None,
password=PASSWORD)
def tearDown(self):
self.driver.quit()
User.objects.all().delete()
def test_login(self):
self.driver.get(BASE_URL + '/accounts/login/?next=/dashboard')
username = self.driver.find_element_by_css_selector(
'.width-half.right #id_username')
username.send_keys(USERNAME)
password = self.driver.find_element_by_css_selector(
'.width-half.right #id_password')
password.send_keys(PASSWORD)
btn_login = self.driver.find_element_by_css_selector(
'.width-half.right .actions button')
# Login
btn_login.click()
# Check on the user dashboard
WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.dash-left')))
Any ideas?
Upvotes: 2
Views: 1919
Reputation: 151441
Based on what you describe happening, here is what I think is going on. Your BASE_URL
is set to connect to your development server which you've started with ./manage.py runserver
. (It can't be connecting to a test server started by TestCase
because TestCase
does not start a server, but it connects to something, which must be your development server.) So Selenium does get a login page because there is a server responding. However, when you try to login, it fails because the user you created is created in the test database created for testing, and not in the development one.
The solution is twofold:
Base you test case on django.test.LiveServerTestCase
like migonzalvar suggested so that a test server is started.
Set your BASE_URL
to connect to this server. The default address is localhost:8081
. See the documentation if you need to change the address.
Upvotes: 1
Reputation: 1685
Try with LiveServerTestCase
instead of TestCase
. It's the way recommended by Django docs.
from django.test import LiveServerTestCase
class MyTestCase(LiveServerTestCase):
...
Also, although not related with your problem, you don't need to execute User.objects.all().delete()
in the tearDown
method. The database is deleted after every test.
Upvotes: 0