To use Page Object Models (POMs) with fixtures in @playwright/test, follow these steps:
import { test } from '@playwright/test';
import { LoginPage } from './LoginPage';
import { HomePage } from './HomePage';
test.extend({
loginPage: async ({ page }, use) => {
const loginPage = new LoginPage(page);
await use(loginPage);
},
homePage: async ({ page }, use) => {
const homePage = new HomePage(page);
await use(homePage);
},
});
Create POM instances in fixture functions: In each fixture definition function, create an instance of the corresponding POM class and return it as the fixture value.
Include POM instances as parameters: In your test functions or hooks where you need access to these POM instances, simply include them as parameters.
import { test } from '@playwright/test';
test('Login and navigate to home page', async ({ loginPage, homePage }) => {
await loginPage.navigate('https://ray.run/login');
await loginPage.login('username', 'password');
const pageTitle = await homePage.getPageTitle();
expect(pageTitle).toBe('Home');
});
By using POMs with fixtures, you can make your tests more readable and maintainable. This approach separates concerns into smaller pieces of code, making it easier to understand and modify when necessary.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].