How to use Playwright Fixtures to improve your testing code
Hello everyone, this is my first article at medium and today I want to show you how to improve your testing code using fixtures in a playwright test.
What is Fixture?
To use playwright/test lib, we’ll need to have a basic knowledge about how the test execution works with this tool and everything starts with fixtures.
Fixtures came to give the test everything it needs and nothing else. Playwright looks to the test declaration before the execution and prepare it all. So, if in the same test file we need to use a single page for one test and browser scope for another one, we handle it using fixtures.
Improving and creating new fixtures
Well, a good thing about fixtures is that we can extend the base of playwright/test lib and override the default fixtures or create another one to generate more and more specifics tests environments.
To make it that, we import playwright as a base, override or create one and just call function “use()” to set what you want to return and use when those fixtures are called in tests files.
Using new fixtures
Now, just import common-fixture.js in tests, the fixture will be executed and when it’s done the “use()” declaration will be returned to fixture name. For example, we override the page fixture to visit my baseURL and set use to return page(from playwright base) and then I follow up with my test.
note: I set https://google.com up as my baseURL.
Fixtures can help us to have more isolated tests environments, reduce the usage of hooks and clean up your code.
You can also use fixtures to create users, access the internal pages, do some pre-processing and another things.
Be free to comment it and