StaticLiveServerTestCase未登录

2024-04-20 05:21:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手。我正在尝试将StaticLiveServerTestCase与Python一起使用来登录到Django管理门户。我正在使用下面的代码。运行以下代码将启动管理门户,但我无法使用凭据登录。如果我遗漏了什么,请告诉我。谢谢!在

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver

class MySeleniumTests(StaticLiveServerTestCase):

    port = 0 
    host = <<my host>>
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.selenium = WebDriver()
        cls.selenium.implicitly_wait(10)

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super().tearDownClass()

    def test_login(self):
        self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('myuser')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('secret')
        self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()

Tags: 代码fromimportselfinputby门户def
2条回答

在安装程序中使用create_superuser方法解决了这个问题。下面的代码解决了这个问题。在

from django.contrib.auth.models import User
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import Client
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import WebDriver
import time
from django.contrib.auth import authenticate

#from apps.digital.models import User


class MyTests(StaticLiveServerTestCase):

    port = 0
    host = 'my host'

    def setUp(self):
        super(MyTests, self).setUp()
        self.selenium = WebDriver()
        self.client = Client()
        self.user = User.objects.create_superuser(username='test', password='Test1234', email='test@test.com', is_active=True)
        self.user.save()

    def tearDown(self):
        self.selenium.quit()
        super(MyTests, self).tearDown()

    def test_login(self):
        self.user = authenticate(username='test', password='Test1234')
        if self.user is not None:  # prints Backend login failed
            self.user = User.objects.get(username='test')
            print(self.user.username)  # prints test
            print(self.user.password)  # prints Test1234
            self.login = self.client.login(username='test', password='Test1234')
            self.assertEqual(self.login, True)
            print("Backend login successful")

            self.selenium.get('%s%s' % (self.live_server_url, '/admin/'))
            username_input = self.selenium.find_element_by_name("username")
            username_input.send_keys(self.user.username)
            password_input = self.selenium.find_element_by_name("password")
            password_input.send_keys('Test1234')
            self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
            time.sleep(1)

        else:
            print("Backend login failed")

您尚未在测试数据库中创建用户。在the example in the docs中,它使用固定装置'user-data.json'。在

{cd2>中的另一个用户}。在

class MySeleniumTests(StaticLiveServerTestCase):

    def setUp(self):
        # Set up data for the whole TestCase
        user = User.objects.create_user(username='username', password='password')
    ...

相关问题 更多 >