我正在努力让Selenium与我的Django项目一起工作。在测试期间,我可以(最终)将其保存到get
页,但是由于某种原因,我无法使其登录。
这是我的(非常简单的)测试用例:
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
通过,test_valid_login
失败,如果我以browser.current_url
为例,它仍指向/ accounts / login /。如果使用browser.page_source
查看页面源,则会看到“您指定的电子邮件地址和/或密码不正确。”在登录表单的错误列表中。
我不知道为什么登录凭据在这里失败,并且我想不出其他任何东西。
这是我在上面提到的browser.page_source
(通过print
之前的assert
捕获)中看到的内容:
<form action="/accounts/login/" class="login auth-form" method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="xLRQ8nZlfocyyQDlJxgLL0PUvsYLLNYNHEZ5awn8cLseQoR2XNQm4TiKOgMvcaS9">
<div class="alert alert-block alert-danger">
<ul>
<li>The e-mail address and/or password you specified are not correct.</li>
</ul>
</div>
<div id="div_id_login" class="form-group">
<div class="">
<input type="email" name="login" value="ricardosoto@gmail.com" placeholder="E-mail address" autofocus="autofocus" class="textinput textInput form-control" required="" id="id_login">
</div>
</div>
<div id="div_id_password" class="form-group">
<div class="">
<input type="password" name="password" placeholder="Password" class="textinput textInput form-control" required="" id="id_password">
</div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign In</button>
</form>
我还包括了测试的相关装置:
import environ
import pytest
import socket
from django.conf import settings
from selenium.webdriver import Remote
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from pytest_django.live_server_helper import LiveServer
from users.tests.factories import UserFactory
env = environ.Env()
@pytest.fixture
def browser() -> Remote:
driver = Remote(
command_executor=env('SELENIUM_HOST', default='http://selenium:4444/wd/hub'),
desired_capabilities=DesiredCapabilities.FIREFOX
)
yield driver
driver.quit()
@pytest.fixture
def test_server() -> LiveServer:
addr = socket.gethostbyname(socket.gethostname())
server = LiveServer(addr)
yield server
server.stop()
@pytest.fixture
def user() -> settings.AUTH_USER_MODEL:
return UserFactory()
作为健全性检查,我尝试添加一些额外的print
语句来检查assert
之前的所有内容:
print('User email: ' + user.email)
print('User is active? ' + str(user.is_active))
print('Password valid? ' + str(user.check_password(password)))
print('URL: ' +browser.current_url)
给我:
User email: rparsons@yahoo.com
User is active? True
Password valid? True
URL: http://172.19.0.6:44025/accounts/login/
我还尝试添加一个绕过Selnium的额外测试,以尝试进一步隔离问题:
class TestAccountLoginView:
def test_valid(self, client, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory):
password = 'password'
user.set_password(password)
user.save()
response = client.post('/accounts/login/', {'login': user.email, 'password': password})
assert response.status_code == 302
这可以按预期工作,并且可以正常登录。
更新
看起来像这样实际上与pytest-django的LiveServer有关。忽略live_server
固定装置,并使用Django的StaticLiveServerTestCase
,我可以很好地登录:
class TestDashboard(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
cls.host = socket.gethostbyname(socket.gethostname())
super().setUpClass()
cls.selenium = Remote(
command_executor='http://selenium:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX
)
cls.selenium.implicitly_wait(10)
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super().tearDownClass()
def test_login(self):
get_user_model().objects.create_user(username='testuser', email='test@example.com', password='password')
self.selenium.get('%s%s' % (self.live_server_url, '/accounts/login/'))
username_input = self.selenium.find_element_by_name("login")
username_input.send_keys('test@example.com')
password_input = self.selenium.find_element_by_name("password")
password_input.send_keys('password')
self.selenium.find_element_by_xpath('//button[@type="submit"]').click()
assert 'Successful' in self.selenium.page_source
不太确定为什么pytest-django的LiveServer会这样做,但是至少我现在有地方可以查看。