I use playwright with pytest. Here are content of my coftest.py file and file with tests test_keplr.py. For some reason, method new_page works as expected in fixture context_use_args_async, but fails everywhere else, except this fixture.
When the program evaluates a line with context.new_page, it opens new page, but endlessly await this coroutine(method).
conftest.py
import pytest
import os
import zipfile
from pathlib import Path
from dotenv import load_dotenv
import pytest_asyncio
from playwright.async_api import async_playwright, Browser, Playwright, BrowserContext
load_dotenv()
@pytest.fixture(scope='session')
def extension_dirs(tmp_path_factory) -> list[str]:
extensions_path = Path(os.getenv('TEST_EXTENSION_PATH'))
temp_dir = tmp_path_factory.mktemp('extensions')
extension_zips = [
path for path in extensions_path.iterdir() if str(path).endswith('.zip')
]
extension_dirs = []
for path in extension_zips:
with zipfile.ZipFile(path, 'r') as zip_ref:
extension_dir = temp_dir / path.name.replace('.zip', '')
zip_ref.extractall(extension_dir)
extension_dirs.append(str(extension_dir))
return extension_dirs
@pytest_asyncio.fixture(scope='session')
async def playwright_async() -> Playwright:
playwright = await async_playwright().start()
yield playwright
await playwright.stop()
@pytest_asyncio.fixture(scope='session')
async def browser_async(playwright_async) -> Browser:
chromium = playwright_async.chromium
browser = await chromium.launch(headless=False, channel='chrome')
yield browser
await browser.close()
@pytest_asyncio.fixture(scope='session')
async def context_async(browser_async) -> BrowserContext:
context = await browser_async.new_context()
await context.new_page()
yield context
await context.close()
@pytest_asyncio.fixture(scope='session')
async def browser_use_args_async(playwright_async, extension_dirs) -> Browser:
extensions_str = ','.join(extension_dirs)
browser = await playwright_async.chromium.launch(
headless=False,
args=[
f'--disable-extensions-except={extensions_str}',
f'--load-extension={extensions_str}'
]
)
yield browser
await browser.close()
@pytest_asyncio.fixture(scope='session')
async def context_use_args_async(browser_use_args_async):
context = await browser_use_args_async.new_context()
yield context
await context.close()
test_keplr.py
import pytest
from playwright.async_api import BrowserContext
from playdegen.async_api.abc import BrowserExtension
from playdegen.async_api.wallets.keplr import Keplr
@pytest.mark.asyncio
async def test_sign_up(context_use_args_async: BrowserContext) -> None:
page = await context_use_args_async.new_page()
...
I tried to use different browsers (chrome and chromium) but it didn`t help.