我正在使用 Playwright 和 Python 构建一个自动化项目。
我想知道使用 pytest 固定装置来初始化页面对象的最佳实践,因此我只需执行一次,然后只在相关测试中调用固定装置。
这是我的 test_login.py 文件:
import pytest
from playwright.async_api import async_playwright
from ...infra.page_objects.login.login_page import LoginPage
@pytest.fixture
async def browser():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
yield browser
await browser.close()
@pytest.fixture
async def login_page_fixture(browser):
page = await browser.new_page()
print("page created")
login_page = LoginPage(page)
print("login_page created")
yield login_page
await page.close()
@pytest.mark.asyncio
async def test_login_with_valid_credentials(login_page_fixture):
login_page = await login_page_fixture
print(f"login page url: {login_page.url}")
这是我的login_page.py 文件:
from playwright.async_api import Page, expect
class LoginPage:
def __init__(self, page: Page):
self.page = page
self.url = 'https://opensource-demo.orangehrmlive.com/web/index.php/auth/login'
self.header = page.locator(".orangehrm-login-branding")
self.header_logo = page.get_by_role("img", name="company-branding")
self.username_field = page.get_by_placeholder("Username")
self.password_field = page.get_by_placeholder("Password")
self.login_button = page.get_by_role("button", name="Login")
self.forgot_password_link = page.get_by_text("Forgot your password?")
async def goto(self) :
await self.page.goto(self.url)
async def verify_page_loaded(self) :
await expect(self.header).to_be_visible()
await expect(self.login_title).to_be_visible()
async def login(self, username: str, password: str) :
await self.fill_username(username)
await self.fill_password(password)
await self.click_login_button()
async def fill_username(self, username:str) :
await self.username_field.fill(username)
async def fill_password(self, password: str) :
await self.password_field.fill(password)
async def click_login_button(self) :
await self.login_button.click()
async def click_forgot_password(self) :
await self.forgot_password_link.click()
当我使用此命令“pytest test_login.py”运行测试时,我收到此错误消息:
login_page_fixture =
@pytest.mark.asyncio
async def test_login_with_valid_credentials(login_page_fixture):
print(f"login page url: {login_page_fixture.url}")
E AttributeError:“协程”对象没有属性“url”
test_login2.py:22:属性错误 =================================================== ================================= 简短的测试摘要信息 =============== =================================================== =============== 失败 test_login2.py::test_login_with_valid_credentials - AttributeError: 'coroutine' 对象没有属性 'url' =================================================== ==================================== 0.05 秒内 1 次失败 =========== =================================================== ======================= sys:1:运行时警告:从未等待过协程“login_page_fixture”
我想获得反馈,也许有人知道什么是正确的解决方案和最佳实践?
谢谢
我建议使用 Playwright 和 TypeScript ,它很容易使用,并且开箱即用,如果您将 playwright 与 TypeScript 一起使用,您可以轻松配置和报告。 您可以关注 Youtube 播放列表 https://youtube.com/playlist?list=PL83cimSRP5ZmwhC6u255huRwSi9tlP-nc&si=-BoooEUvf-ZM4EdP 了解更多信息。