New to storybook, playwright integration? #24835
-
Hi there. I'm new to the Storybook ecosystem, and thinking about migrating our stories and tests. For e2e tests, we use Playwright, and have also started to write playwright component tests. What is the status of the integration between storybook with playwright component tests - is this possible? Or maybe to understand this better, how are interaction tests written in storybook - and can we use playwright API to write these tests? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 16 replies
-
Hi, what is stopping you to load story and interact with it using playwright? I just started and the only thing I see so far, that from the storybook UI, you have to change the context to the id="storybook-preview-iframe" or just load the full story url in playwright |
Beta Was this translation helpful? Give feedback.
-
If you don't mind the story tests being run in a separate process, the test-runner uses Playwright. I haven't tried it, but I think you should be able to pass a component's composed story to the Playwright |
Beta Was this translation helpful? Give feedback.
-
Hey there @alexbjorlig! Let me add to what @Marklb said and try to explain the state of testing with Storybook. With Storybook, you can define stories for each state of your component, and that is a combination of two things:
While you could potentially do what @freed00m mentioned, Storybook provides a test-runner which is based on Jest (as a runner) and Playwright (as environment), which essentially turns all of your stories into Playwright tests automatically. It will notify you whether there are failures in the rendering step or the play function step of your stories. The test-runner offer hooks like If you do not want to use the test-runner, but rather want to use Jest/Vitest/Cypress CT/Playwright CT directly, you can use what we call "portable stories", which is a way to use Storybook's // Button.stories.ts
import { Button } from './Button'
const meta = {
component: Button
}
export default meta;
export const Primary = {
args: { label: 'Hello!' }
} // Button.composed.stories.tsx (or whatever name you prefer)
// Playwright CT does not allow you to create components in the same file it tests, therefore this needs to be in a separate file
import { composeStories } from '@storybook/react'
import * as stories from './IconButton.stories'
export default composeStories(stories) // Button.test.spec.tsx
import stories from './Button.composed'
import { test, expect } from '@playwright/experimental-ct-react';
describe('<Button />', () => {
test('renders', async ({ mount }) => {
// this will render the button alongside its decorators, args, etc.
const component = await mount(<stories.Primary />);
await expect(component).toContainText('Hello!');
})
}) In this scenario, composeStories will return you renderable components that will include the I don't know what you mean by migrating your stories and tests, but I'd say Storybook keeps great value because of its documentation nature, development experience and addon ecosystem, so I wouldn't recommend migrating everything, but I can see value in being able to use Playwright's APIs for testing your components, although the play function could be quite useful for your use cases as well. |
Beta Was this translation helpful? Give feedback.
-
Thanks for such great answers, currently reading through it all and trying to understand the pros and cons 🤓 Will update this thread with my findings, when I have progressed more with the actual implementation. |
Beta Was this translation helpful? Give feedback.
Hey there @alexbjorlig!
Let me add to what @Marklb said and try to explain the state of testing with Storybook. With Storybook, you can define stories for each state of your component, and that is a combination of two things:
A combination of decorators, the render function of your story, and its args being applied to it.
A function that gets executed after the story renders. Useful to get to states that need user interactions, such as filling a form, clicking on a dropdown for its open state, etc. Additionally, the play function can also assert things via
expect
coming from@storybook/jest
, which show a failure in the addon panel if the assertions do not match.W…