OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to save django db data in the middle of a playwright test?

  • Thread starter Thread starter Barney Szabolcs
  • Start date Start date
B

Barney Szabolcs

Guest
I'm having trouble figuring out how to call the "sync" create_user().

Error message:

Code:
django.core.exceptions.SynchronousOnlyOperation: 
You cannot call this from an async context - use a thread or sync_to_async.

However, when I use sync_to_async, it cannot be awaited because this is not an async function...

Code:
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()

If you have a suggestion, please let me know, my hair starts to fall out. 🙏
<p>I'm having trouble figuring out how to call the "sync" <code>create_user()</code>.</p>
<p>Error message:</p>
<pre><code>django.core.exceptions.SynchronousOnlyOperation:
You cannot call this from an async context - use a thread or sync_to_async.
</code></pre>
<p>However, when I use sync_to_async, it cannot be awaited because this is not an async function...</p>
<pre class="lang-py prettyprint-override"><code>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()
</code></pre>
<p>If you have a suggestion, please let me know, my hair starts to fall out. 🙏</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top