-
Notifications
You must be signed in to change notification settings - Fork 993
[Question]: Best practices for using page-objects as pytest fixtures and for implementing async methods #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can create custom Pytest fixtures with your POMs like that: @pytest.fixture
def my_page(page: Page):
return MyPage(page) See here: https://docs.pytest.org/en/6.2.x/fixture.html#back-to-fixtures Playwright has no best practises in this regard for Python yet, so it totally depends on your project. If you feel comfortable with your setup, understand it and provides you all the flexibility, then its good enough! |
Yes, but when I want to use methods I've defined in the page object class, in the scope of the test, it doesn't recognize the methods and the attributes. |
You mean typing wise? Are you annotating it with types? def test_foo(my_page: MyPage) -> None:
my_page.foo() |
I want to have a fixture for login_page object, so I won't have to initialize it every test from scratch and duplicate my code. `@pytest.fixture @pytest.mark.asyncio I get this error: |
Oh I see, you are not using our pytest-playwright plugin since you want to leverage the async version of Playwright. Yes you could do it like that. You can modify your fixture to this and then it should work: @pytest.fixture
async def login_page(browser):
page = await browser.new_page()
login_page = LoginPage(page)
yield login_page see e.g. here. |
@mxschmitt do you mean like this:
I tried to run it this way and encoutered the following error:
|
Why are you using async in the first place and not our pytest plugin? It provides a lot of things already out of the box: https://playwright.dev/python/docs/test-runners You need to manually create a browser fixture, otherwise your login_page fixture doesn't know how to get the browser. Something like this: @pytest.mark.asyncio
async def browser():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
yield browser and then you can change your test to e.g. this: @pytest.mark.asyncio
async def test_login_with_valid_credentials(login_page):
# Go to login page
await login_page.goto()
# Verify Login Page loaded
await login_page.verify_page_loaded()
# Fill in user name
await login_page.fill_username(user.username)
# Fill in password
await login_page.fill_password(user.password)
# Click Login button
await login_page.click_login_button()
|
Closing as per above. When creating own async pytest fixtures (not using pytest-playwright) we can't provide any official support. See here for our sync implementation, which should be very similar for async. And see microsoft/playwright-pytest#74 |
Your question
Hi!
I'm practicing playwright/pytest.
I wonder what would be the best practices for:
This is the high-level structure of my project:
PROJECT
│ README.md
│
└───src
│
├───infra
│ │ init.py
│ │
│ └───page_objects
│ └───login
│ │ login_page.py
│
└───tests
└───sanity_tests
│ test_login.py
The text was updated successfully, but these errors were encountered: