Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to fix this playwright error: ValueError: The future belongs to a different loop than the one specified as the loop argument

Getting this error:

ValueError: The future belongs to a different loop than the one specified as the loop argument.

Future exception was never retrieved future: <Future finished exception=TargetClosedError('Target page, context or browser has been closed')> playwright._impl._errors.TargetClosedError: Target page, context or browser has been closed

Content of "conftest.py" file

import pytest

import pyotp

from playwright.async_api import async_playwright


browsers = ["chromium"]


@pytest.fixture(scope="session", params=browsers)
async def browser_context_creation_admin(request):
    browser_type = request.param
    async with async_playwright() as p:
        browser = await getattr(p, browser_type).launch(headless=False, slow_mo=300)
        context = await browser.new_context()
        page = await context.new_page()
        page.set_default_timeout(20000)

        await page.goto("")
        await page.get_by_label("Username").click()
        await page.get_by_label("Username").fill("")
        await page.get_by_role("button", name="Next").click()
        await page.get_by_role("link", name="Select").first.click()
        totp = pyotp.parse_uri()
        generated_otp = totp.now()
        await page.get_by_label("Enter code").fill(generated_otp)
        await page.get_by_role("button", name="Verify").click()
        await page.get_by_role("link", name="Select").nth(1).click()
        await page.get_by_label("Password").fill("")
        await page.get_by_role("button", name="Verify").click()
        await page.get_by_role("button", name="I Accept").click()

        yield context

        await context.close()
        await browser.close()




@pytest.fixture()
async def login_set_up_admin(browser_context_creation_admin):
    admin_context = browser_context_creation_admin
    admin_page = await admin_context.new_page()
    await admin_page.goto("")
    admin_context.set_default_timeout(20000)
    yield admin_page
    await admin_page.close()

Content of test_login.py file

async def test_login_with_valid_creds_admin(login_set_up_admin):
    admin_page = login_set_up_admin

    await admin_page.locator('xpath=//button[@id="icon-btn-3"]//div[@class="notification"]//*[name()="svg"]//'
                       '*[name()="path" and contains(@d,"M16,2A14,1")]').click()

    assert await admin_page.get_by_role("button", name="Logout").is_visible()

like image 424
Basavaraj Lamani Avatar asked Oct 13 '25 01:10

Basavaraj Lamani


1 Answers

Finally able to fix this issue.

This issue occurred because of the incompatibility between pytest 8.0.0 version and pytest-asyncio 0.23.7.

Installed pytest 8.0.0 and pytest-asyncio==0.20.2, Now browser context and tests are running fine.

like image 106
Basavaraj Lamani Avatar answered Oct 14 '25 17:10

Basavaraj Lamani