Python StaticLiveServerTestCase未登录

Python StaticLiveServerTestCase未登录,python,django,selenium,django-testing,django-tests,Python,Django,Selenium,Django Testing,Django Tests,我是python新手。我正在尝试使用Python的StaticLiveServerTestCase登录到Django管理门户。我正在使用下面的代码。运行以下代码将启动管理门户,但我无法使用凭据登录。如果我遗漏了什么,请告诉我。谢谢 from django.contrib.staticfiles.testing import StaticLiveServerTestCase from selenium.webdriver.firefox.webdriver import WebDriver cl

我是python新手。我正在尝试使用Python的
StaticLiveServerTestCase
登录到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()
从django.contrib.staticfiles.testing导入StaticLiveServerTestCase
从selenium.webdriver.firefox.webdriver导入webdriver
类MySeleniumTests(StaticLiveServerTestCase):
端口=0
主机=
@类方法
def设置等级(cls):
super().setUpClass()
cls.selenium=WebDriver()
cls.selenium.implicitly_wait(10)
@类方法
def拆卸类(cls):
cls.selenium.quit()
super().tearDownClass()
def测试_登录(自我):
self.selenium.get(“%s%s%”(self.live\u server\u url,/login/))
username\u input=self.selenium.通过名称(“用户名”)查找元素
用户名\输入。发送\密钥('myuser')
password\u input=self.selenium.通过名称(“密码”)查找元素
密码\u输入。发送\u密钥('secret')
self.selenium.by_xpath('//input[@value=“Log in”]”)查找_元素。单击()

您尚未在测试数据库中创建用户。在中,它使用fixture
“user-data.json”

另一个选项是在
setUp
函数中创建用户

class MySeleniumTests(StaticLiveServerTestCase):

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

在安装程序中使用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")

谢谢阿拉斯代尔,非常感谢。我试图合并这个,但我发现我仍然无法登录。类MyTests(StaticLiveServerTestCase):端口=0主机='my shost'定义安装程序(self):超级(MyTests,self)。安装程序()self.selenium=WebDriver()self.user=user.objects.create_user(用户名='test',密码='Test@123')def tearDown(self):self.selenium.quit()super(MyTests,self.tearDown())def test_login(self):self.selenium.get(“%s%s%”(self.live_server_url,/admin/))请不要在注释中发布代码,无法读取。确定。我刚刚发布了我的更新代码作为评论,你能随时查看吗?在安装程序中使用create_superuser方法解决了这个问题。很高兴你找到了答案
StaticLiveServerTestCase
基于
TransactionTestCase
,因此不能使用Django的
TestCase
中的
setUpTestData