我无法弄清楚如何调用“同步”
create_user()
。
错误信息:
django.core.exceptions.SynchronousOnlyOperation:
You cannot call this from an async context - use a thread or sync_to_async.
但是,当我使用sync_to_async时,无法等待它,因为这不是异步函数...
from functools import wraps
from django.test import TestCase
from playwright.sync_api import sync_playwright
def playwright_test(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
with sync_playwright() as self.p:
self.browser = self.p.chromium.launch()
self.page = self.browser.new_page()
try:
result = func(self, *args, **kwargs)
finally:
self.browser.close()
return result
return wrapper
class TC(TestCase):
@playwright_test
def test_login(self):
self.page.goto(self.host)
self.page.fill('input[type="email"]', '[email protected]')
self.page.fill('input[type="password"]', 'TestLogin')
self.page.click('text="Login"')
# expect "Incorrect Credentials" message (no user created yet)
assert "Incorrect Credentials" in self.page.content()
User = get_user_model()
User.objects.create_user('[email protected]', password='TestLogin')
# Login again, this time successfully
self.page.fill('input[type="email"]', '[email protected]')
self.page.fill('input[type="password"]', 'TestLogin')
self.page.click('text="Login"')
assert "Login successful. Welcome back!" in self.page.content()
如果您有建议,请告诉我,我的头发开始脱落。 🙏
您可以通过以下方式避免错误消息:
也许这足以让您继续(我几乎在任何地方都使用这种解决方法)。
至于测试本身,你可以尝试这样的事情:
# conftest.py
# https://github.com/microsoft/playwright-python/issues/439
# https://github.com/microsoft/playwright-pytest/issues/29#issuecomment-731515676
os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true")
# test_login.py
import pytest
from django.contrib.auth import get_user_model
@pytest.fixture
def user_email():
return "[email protected]"
@pytest.fixture
def user_password():
return "TestLogin"
@pytest.fixture
def create_user(user_email, user_password):
return get_user_model().objects.create_user(user_email, password=user_password)
@pytest.fixture
def host():
return "?"
@pytest.fixture
def try_login(page, host, user_email, user_password):
page.goto(host)
page.fill('input[type="email"]', user_email)
page.fill('input[type="password"]', user_password)
page.click('text="Login"')
return page
class TestLogin:
def test_login_fail_no_user(self, page, user_email, user_password):
page.goto(host)
page.fill('input[type="email"]', user_email)
page.fill('input[type="password"]', user_password)
page.click('text="Login"')
# expect "Incorrect Credentials" message (no user created yet)
assert "Incorrect Credentials" in page.content()
def test_login_success(self, create_user, page, host, user_email, user_password):
page.goto(host)
page.fill('input[type="email"]', user_email)
page.fill('input[type="password"]', user_password)
page.click('text="Login"')
# Login again, this time successfully
assert "Login successful. Welcome back!" in page.content()
或者干一点:
@pytest.fixture
def try_login(page, host, user_email, user_password):
page.goto(host)
page.fill('input[type="email"]', user_email)
page.fill('input[type="password"]', user_password)
page.click('text="Login"')
return page
class TestLogin:
def test_login_fail_no_user(self, page, try_login):
# expect "Incorrect Credentials" message (no user created yet)
assert "Incorrect Credentials" in page.content()
def test_login_success(self, create_user, page, try_login):
# Login again, this time successfully
assert "Login successful. Welcome back!" in page.content()