reg-viz / storycap

A Storybook Addon, Save the screenshot image of your stories :camera: via puppeteer.
https://www.npmjs.com/package/storycap
MIT License
716 stars 88 forks source link

Proposal: expose `triggerScreenshot()` as JS API #518

Open lacolaco opened 2 years ago

lacolaco commented 2 years ago

Abstract

Motivation

Storybook v6.4 added Interactions feature which allows us to write stories with actual browser events from synthetic user interactions. Currently, Storycap can capture screenshots of the moment after finished the interaction.

I think it would be more effective visual testing to have a capability to capture at the steps during the interaction. For example, assume a password input form with a property for toggle text visibility. Its story and an interaction function maybe become like below:

export const PasswordInput = {
  render: () => <MyPasswordInput />,
  play: async () => {
    // ...
    const input = await findByRole('....');
    await userEvent.type(input, 'password');
    // == CAPTURE 1 ==
    await triggerScreenshot('hidden password')
    const toggle = await findByRole('button');
    await userEvent.click(toggle);
    // == CAPTURE 2 ==
    await triggerScreenshot('visible password')
  },
}

APIs

I think Storycap has already triggerScreenshot() function as an internal function. It is only needed to expose that as a public API. But it should be an Async Function that returns Promise for using in play() function.

MH4GF commented 1 year ago

I want this feature too. There are many cases where I want to explicitly declare the screenshot timing in the play function.