Reusable test setups using fixtures
After creating the authentication test, you might be thinking: what if I want to define a test for creating a new post now? We would have to first sign up, then log in, then create the post. This is quite tedious and the more complex our tests get; the more tedious defining tests would get. Fortunately, Playwright has a solution for these kinds of problems. Playwright introduces a concept called fixtures, which are contexts for the test that can contain reusable functions. For example, we could define an auth
fixture to provide sign-up and log-in functions to all tests.
When we used Jest, we were using before/after hooks to prepare the common environment for multiple tests. Fixtures have some advantages over before/after hooks. Mainly, they encapsulate setup and teardown in the same place and are reusable between test files, composable, and more flexible. Additionally, fixtures are provided on demand, which means that Playwright will only set...