Search code examples
pythondjangodjango-testing

Authentication failed in Django test


In Django I tried to create a user and then I tried to login that user using selenium , but when I run the test it failed , It was showing authentication error. Here is my code :

class LoginFunctionalTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)
        # self.browser.get('http://localhost:8000')

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

    def test_login_page(self):
        self.browser.get('http://localhost:8000')

        self.assertIn('Hiren->Login', self.browser.title)

    def test_login_form(self):
        self.browser.get('http://localhost:8000')
        user = User.objects.create_user('testHiren', '[email protected]', 'testPass')
        user.save()
        username = self.browser.find_element_by_id('username-id')
        password = self.browser.find_element_by_id('password-id')
        submit = self.browser.find_element_by_id('login-button')
        username.send_keys('testHiren')
        password.send_keys('testPass')
        submit.click()
        time.sleep(10) # if authentication successful its redirects to /dashboard
        location = self.browser.current_url
        self.assertEqual('http://localhost:8000/dashboard', location)

Any idea why its failing ?


Solution

  • So I just figured it out this FN test is using default database rather then test database, so I changed the flow