Playwright Fixtures - Little Walkthrough

Rate this content
Bookmark

What are fixtures? How can I create one? How can I use it? I will answer all this questions with a small but explanatory live code example.

This talk has been presented at TestJS Summit 2023, check out the latest edition of this JavaScript Conference.

FAQ

Control is important because it allows you to decide which methods and settings are used in your tests. This leads to a more manageable and predictable testing environment, enhancing both test reliability and reusability.

The after block in a Playwright fixture is used to perform any cleanup actions needed after the test has run. This can include logging information or resetting the environment.

The before block in a Playwright fixture is used to set up the necessary environment and conditions before the test runs. It ensures that everything is in place for the test to execute correctly.

A fixture in Playwright is a small environment containing methods and settings that are defined by the user. It can be compared to small balls inside a ball pit, where Playwright is the ball pit and the fixtures are the balls.

Fixtures are used in Playwright to control and manage the testing environment. They allow users to define reusable methods and settings, making the tests more readable and maintainable.

Examples of fixtures in Playwright include the page fixture and the context browser fixture. These fixtures are predefined, but users can also create their own custom fixtures.

Custom fixtures improve test reusability by allowing you to define reusable methods and settings. This means you can use the same fixture for multiple tests without having to rewrite the setup each time.

Using page objects in Playwright tests makes the tests more readable and maintainable. Page objects encapsulate the methods and actions that can be performed on a page, making the test code cleaner and easier to understand.

To create a custom fixture in Playwright, you need to define a function that tells Playwright to add your fixture. This function should include a before block, the test itself, and an after block.

To integrate custom methods into a Playwright fixture, you need to define these methods in a page object and then create a fixture that includes these methods. This fixture can then be imported and used in your tests.

Lia Moreira
Lia Moreira
6 min
07 Dec, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Fixtures in Playwright are like small environments where you define methods and control the testing process. By creating reusable fixtures, you can make tests more readable and reusable. The fixture structure consists of a before block, the test, and an after block. Using fixtures allows you to access methods created in a page object, improving code readability and reusability.

1. Introduction to Fixtures

Short description:

Hello, everyone. I am Lia, the gatekeeper of the application's quality. My goal is to help you understand fixtures and how to create your own. Fixtures are like small environments in Playwright, where you define methods and stuff. Playwright gives us the opportunity to create our own fixtures, providing control over the testing process.

Hello, everyone. I am Lia. I hope you are having fun today. My job description at Bleep is quality assurance engineer, but I like to describe myself as the gatekeeper of the application's quality. My goal here is, at the end of this seven minutes more or less, you get a better understanding of what is a fixture and how you can create your own.

So let's start from the beginning. What is a fixture? Well, I like to describe fixtures in Playwright just like a little small environment where exists methods and stuff that I defined. I use these analogies like, imagine a ball pit where Playwright is the ball pit and fixtures are the small balls that are inside of the ball pit.

And why? Why do you use fixtures? So Playwright is fixture based. Everything that we use in Playwright is a fixture. Page is a fixture. Context browser is a fixture. And as I said previous, Playwright gives us the opportunity to create our own. And you can ask, okay, if Playwright gives us the opportunity, sorry, if Playwright gives us the fixture already, why do I need to create my own? Well, the answer is control. But okay, this is not working. Of course we need to have problems. Okay, control. Okay. Control of what? Well, let's see a little example.

2. Creating Reusable Fixtures

Short description:

To make the test more readable and reusable, I pass it to a page object with my own methods. By creating a fixture, I can make these methods available for use. The fixture structure consists of a before block, the test, and an after block. Instead of importing test and using page, I import and use my fixture on the test. This allows me to access the methods I created in the page object, making the code more readable and reusable. Playwright fixtures provide control over the methods used in the fixture, creating a small and controllable environment.

So this is a test that I created, and you can see that it's using the page fixture. I'm importing test from Playwright. I have a before it. I am checking for some titles and filling some forms. But this is not really reusable and you cannot really understand if you don't have the context of the application, what is being done here.

So how can I create this more easy to read and more reusable for everyone that needs to add tests to use it? So the answer is, I'm going to pass this to a page object that has my own methods. And you can see here that I have getTitle, clickButtons, fillForms. So this is my methods, my page objects, but this only by itself does not make them available on the page fixture.

So what I need to do to make them available to use is, well, create a fixture. So the structure here is, I need to have a function that is going to say to Playwright, add my fixture to the land of the feature. So test is the land of the features and I'm going to extend my own features. And in my fixture definition, there is going to be my test. You can use other fixtures like a fixture section to do stuff, but the main stuff here is the structure. So the structure for the fixture is going to be the before block, the test, and then the after block. So what I'm doing here is, if you look at the previous, the initial test, I have a before hook and comparing to my fixture, the same before hook is there. Then I'm going to use my methods in the initial page and after the run, I'm going to console.log something.

So how can I use this in my test? So instead of importing test, instead of using page, now what I'm going to do is import my fixture and use my fixture on the test and import my test that is the actual fixture. And as you can see there, it has access to my methods that I created in the page object. It's a little more readable and it's very reusable because if I want to have, let's say, another 10 tests that go through this, you can just reuse it instead of just having to recode everything. So the control that PlayWrite Fixtures gives us is I can decide what methods do I need to use or what I want to use in my fixture. I have a really small environment that is very controllable and very reusable. So, I hope that this gives you a little bit more of insight on fixtures and how you can use it in your tests. And that's it guys.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Atomic Deployment for JS Hipsters
DevOps.js Conf 2024DevOps.js Conf 2024
25 min
Atomic Deployment for JS Hipsters
This Talk discusses atomic deployment for JavaScript and TypeScript, focusing on automated deployment processes, Git hooks, and using hard links to copy changes. The speaker demonstrates setting up a bare repository, configuring deployment variables, and using the post-receive hook to push changes to production. They also cover environment setup, branch configuration, and the build process. The Talk concludes with tips on real use cases, webhooks, and wrapping the deployment process.
Effective Performance Testing to your Server with Autocannon
TestJS Summit 2021TestJS Summit 2021
36 min
Effective Performance Testing to your Server with Autocannon
Top Content
Tamar is an experienced code writer and architect with expertise in Node.js. Performance testing can be confusing, but understanding terms like throughput and the 99th percentile is crucial. The 99th percentile is important for making commitments and ensuring customer satisfaction. AutoCanon is a powerful tool for simulating requests and analyzing server performance. It can be installed globally or used as a library in Node.js. Autocannon is preferred over Gatling for performance testing and can be integrated with end-to-end tests in Cypress.
Delightful Integration Tests With Testcontainers
TestJS Summit 2022TestJS Summit 2022
21 min
Delightful Integration Tests With Testcontainers
Top Content
Testing is crucial for development and production, with integration tests becoming more popular. Test containers is a library that integrates with Docker to create reliable test environments. It is flexible and can be used with various frameworks and test libraries. The IDE setup involves configuring the container and connecting it to the application. Test containers can be used for complex operations and allows running tests with real dependencies.
Visual Regression with Puppeteer, Playwright and Cypress
TestJS Summit 2021TestJS Summit 2021
9 min
Visual Regression with Puppeteer, Playwright and Cypress
Top Content
Hello, I'm Rainer Haneckamp, a trainer and consultant at Angular Architects. In this talk, we'll explore visual regression testing using tools like Puppeteer, Playwright, and Cypress. We'll learn how to use Storybook and Puppeteer with Jest for visual regression testing. We'll also see how Jest and Playwright can be used together for visual regression testing. Finally, we'll discover how to use Cypress for visual regression testing. Thank you for watching!
Playwright Can Do This?
TestJS Summit 2022TestJS Summit 2022
23 min
Playwright Can Do This?
Playwright is a powerful tool for end-to-end testing, offering support for all major browsers and platforms. It provides features like parallelization, built-in waiting, and assertions. Playwright allows for running tests on multiple browsers with a single command and has functionality for generating tests and performing visual regression testing. It also enables the manipulation of the network layer and loading internals of web pages. Best practices include using short and idempotent scripts, splitting user account flows into separate tests, and cleaning up after each test case.
The Lazy Developer Guide: How to Automate Code Updates?
DevOps.js Conf 2022DevOps.js Conf 2022
22 min
The Lazy Developer Guide: How to Automate Code Updates?
Code automations can save time and effort in development tasks. There are tools and examples available for automating tasks like updating dependencies and code formatting. Automation allows teams to focus on valuable work and improves overall performance. Deciding when to automate depends on the impact and type of code. The last automated task discussed was applying translation updates to multiple projects.

Workshops on related topic

Automated accessibility testing with jest-axe and Lighthouse CI
TestJS Summit 2021TestJS Summit 2021
85 min
Automated accessibility testing with jest-axe and Lighthouse CI
Workshop
Bonnie Schulkin
Bonnie Schulkin
Do your automated tests include a11y checks? This workshop will cover how to get started with jest-axe to detect code-based accessibility violations, and Lighthouse CI to validate the accessibility of fully rendered pages. No amount of automated tests can replace manual accessibility testing, but these checks will make sure that your manual testers aren't doing more work than they need to.
Automated Testing Using WebdriverIO
TestJS Summit 2022TestJS Summit 2022
163 min
Automated Testing Using WebdriverIO
Workshop
Kevin Lamping
Kevin Lamping
In this workshop, I cover not only what WebdriverIO can do, but also how you'll be using it day-to-day. I've built the exercises around real-world scenarios that demonstrate how you would actually set things up. It's not just "what to do," but specifically "how to get there." We'll cover the fundamentals of Automated UI testing so you can write maintainable, useful tests for your website and/or web app.
JS Security Testing Automation for Developers on Every Build
TestJS Summit 2021TestJS Summit 2021
111 min
JS Security Testing Automation for Developers on Every Build
WorkshopFree
Oliver Moradov
Bar Hofesh
2 authors
As a developer, you need to deliver fast, and you simply don't have the time to constantly think about security. Still, if something goes wrong it's your job to fix it, but security testing blocks your automation, creates bottlenecks and just delays releases...but it doesn't have to...

NeuraLegion's developer-first Dynamic Application Security Testing (DAST) scanner enables developers to detect, prioritise and remediate security issues EARLY, on every commit, with NO false positives/alerts, without slowing you down.

Join this workshop to learn different ways developers can access Nexploit & start scanning without leaving the terminal!

We will be going through the set up end-to-end, whilst setting up a pipeline, running security tests and looking at the results.

Table of contents:
- What developer-first DAST (Dynamic Application Security Testing) actually is and how it works
- See where and how a modern, accurate dev-first DAST fits in the CI/CD
- Integrate NeuraLegion's Nexploit scanner with GitHub Actions
- Understand how modern applications, APIs and authentication mechanisms can be tested
- Fork a repo, set up a pipeline, run security tests and look at the results
Security Testing Automation for Developers on Every Build
GraphQL Galaxy 2021GraphQL Galaxy 2021
82 min
Security Testing Automation for Developers on Every Build
WorkshopFree
Oliver Moradov
Bar Hofesh
2 authors
As a developer, you need to deliver fast, and you simply don't have the time to constantly think about security. Still, if something goes wrong it's your job to fix it, but security testing blocks your automation, creates bottlenecks and just delays releases, especially with graphQL...but it doesn't have to...

NeuraLegion's developer-first Dynamic Application Security Testing (DAST) scanner enables developers to detect, prioritise and remediate security issues EARLY, on every commit, with NO false positives / alerts, without slowing you down.

Join this workshop to learn different ways developers can access NeuraLegion's DAST scanner & start scanning without leaving the terminal!

We will be going through the set up end-to-end, whilst setting up a pipeline for a vulnerable GraphQL target, running security tests and looking at the results.

Table of contents:
- What developer-first DAST (Dynamic Application Security Testing) actually is and how it works
- See where and how a modern, accurate dev-first DAST fits in the CI/CD
- Integrate NeuraLegion's scanner with GitHub Actions
- Understand how modern applications, GraphQL and other APIs and authentication mechanisms can be tested
- Fork a repo, set up a pipeline, run security tests and look at the results