vendredi 17 mai 2019

Selenium unable to login to Django LiveServerTestCase

I'm struggling to get Selenium working with my Django project. I can (finally) get it to get pages during testing, but I'm unable to get it to login for some reason.

This is my (very simple) test case:

import pytest

from django.conf import settings
from django.contrib.auth import get_user_model
from django.test.client import Client

from pytest_django.live_server_helper import LiveServer
from selenium.webdriver import Remote

from users.tests.factories import UserFactory


pytestmark = pytest.mark.django_db


class TestDashboard:
    def test_site_loads(self, browser: Remote, test_server: LiveServer):
        browser.get(test_server.url)

        assert 'Welcome' in browser.title

    def test_valid_login(self, browser: Remote, test_server: LiveServer, user: settings.AUTH_USER_MODEL):
        password = 'testpassword'
        user.set_password(password)
        user.save()

        browser.get(test_server.url + '/accounts/login/')
        browser.find_element_by_name('login').send_keys(user.email)
        browser.find_element_by_name('password').send_keys(password)
        browser.find_element_by_css_selector('button[type="submit"]').click()
        browser.implicitly_wait(2)

        assert f'Successfully signed in as {user.username}' in browser.page_source

test_site_loads passes, test_valid_login fails, and if I example browser.current_url it's still pointing at /accounts/login/. If I look at the page source using browser.page_source, I can see "The e-mail address and/or password you specified are not correct.".

I have no idea why the login credentials are failing here.

Aucun commentaire:

Enregistrer un commentaire