Test Fixtures | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore the concept of test fixtures in the Playwright framework . A test fixture is a function that serves as a precondition or teardown for tests, offering more capabilities than traditional test hooks like before and after . Key Concepts Test Fixture: A reusable function that sets up conditions for tests. Before Hooks: Traditional hooks that require restructuring tests when different tokens are needed. Fixture Advantages: Avoids code duplication and allows for cleaner test organization. Creating a Test Fixture To create a test fixture: Create a new file named fixtures.ts in the utils folder. Import the test function from Playwright and rename it to base . Extend the base function to define your fixture. Use the async function syntax to define the fixture's behavior. Fixture Implementation In the fixture: Pass an object with dependencies as the first argument. Use the use function as the second argument to wrap test execution. Code before the use line executes as a precondition, while code after it executes afterward. Example Usage To utilize the fixture: Export the updated test function as export const test = testBase; . Import this test function in your test files instead of the default Playwright test. Call the fixture in your tests to access the configured request handler. By implementing fixtures, we achieve a structured approach to testing, akin to a three-layer architecture : the top layer for tests, the middle for fixtures, and the bottom for request handlers. That's a summary of creating and using test fixtures in Playwright! See you in the next lesson.