Django: Second Selenium Test Failing with 500 Server Error

2.5k views Asked by At

I am trying to add some selenium tests to my Django project, but the second test always fails with a Server Error (500) . Since both tests start exactly the same, I figure it must have something to do with the setUp and tearDown methods. Can someone help? Thanks.

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User 
from selenium.webdriver.support.ui import Select
class UserTest(LiveServerTestCase):
    def setUp(self):
        User.objects.create_user(username='user', password='pass', email='[email protected]')
        self.browser = webdriver.Chrome()

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

    def changeSelector(self, browser, value):
        mealSelector = Select(browser.find_element_by_id('mealsToday'))
        mealSelector.select_by_visible_text(str(value))

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.timeout(5)
        self.assertIn('Animals', self.browser.title)
        # Log in
        login_button = self.browser.find_element_by_id('login').click()
        self.browser.find_element_by_id('id_username').send_keys('user')
        self.browser.find_element_by_id('id_password').send_keys('pass')

    def timeout(self, time_to_sleep):
        import time
        time.sleep(time_to_sleep)

    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()

Edit: I should mention that the first test works fine and returns success. Any test after the first one fails right on starting up with the 500 error.

Edit 2: What I see when I run my tests:

======================================================================
FAIL: test_two_test (functional_tests.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/functional_tests/tests.py", line 34, in test_two_test
    self.login_user()
  File "/functional_tests/tests.py", line 20, in login_user
    self.assertIn('Animals', self.browser.title)
AssertionError: 'Animals' not found in 'http://localhost:8081/'

Even this minimal code fails:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User 
from selenium.webdriver.support.ui import Select
class UserTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

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

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.assertIn('Animals', self.browser.title)
    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()

The second time the get is called in the second method I can see that the 500 Error is there and that nothing is correctly loaded. Why would this be?

3

There are 3 answers

0
skaz On BEST ANSWER

After some code to be able to show me errors (the testing suite sets DEBUG=False to closer simulate the real env) by setting DEBUG=True

Then I saw that the code was bombing because a row wasn't there that the system expected. This is because I add this row in a migration script. This passes the first test because all migration scripts are run at the beginning of testing, but after all data is deleted after the first test, it is never added again.

7
alecxe On

It's difficult to say without seeing the complete traceback, but, it could be because of the create_user() call - it fails to create a user with an existing username. Try moving the create_user() under the setUpClass():

class UserTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        User.objects.create_user(username='user', password='pass', email='[email protected]')
        super(UserTest, cls).setUpClass()

    def setUp(self):
        self.browser = webdriver.Chrome()
0
Valentyn On

Maybe, it will be useful for somebody.

I have problems with testing with Selenium using LiveServerTestCase, Debug was True, all about last answers were about was OK.

But, earlier I tried to deploy my web-application on Heroku, and I have such line of code in my settings.py:

django_heroku.settings(locals())

But when I removed it, I didn't catch this error.