Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a way to know browser name in postRender #220

Closed
vladnicula opened this issue Nov 16, 2022 · 3 comments
Closed

Need a way to know browser name in postRender #220

vladnicula opened this issue Nov 16, 2022 · 3 comments

Comments

@vladnicula
Copy link

vladnicula commented Nov 16, 2022

I'm following along a really nice article from https://storybook.js.org/blog/interaction-testing-with-storybook/

In it, there are two things that work well in isolation:

  1. The cross browser testing
# cross-browser testing
npm run test-storybook --browsers chromium firefox webkit

and
2. Snapshot testing via jest-snapshot-test

// .storybook/test-runner.js
const { toMatchImageSnapshot } = require('jest-image-snapshot');

const customSnapshotsDir = `${process.cwd()}/__snapshots__`;

module.exports = {
  setup() {
    expect.extend({ toMatchImageSnapshot });
  },
  async postRender(page, context) {
    const image = await page.screenshot();
    expect(image).toMatchImageSnapshot({
      customSnapshotsDir,
      customSnapshotIdentifier: context.id,
    });
  },
};

However these two don't work togather. If I try to do a screenshot of a modest button with the css style all:unset, we will get a snapshot failed error because there are small pixel differences between browsers.

Ideally, I'd like to prefix the snapshotIdentifier with the browser name, just like playwright does it. To do so, I think I need the browserName https://playwright.dev/docs/api/class-fixtures#fixtures-browser-name variable to be accesible in the postRender.

@vladnicula
Copy link
Author

Looking at the code it might be a simple extension of the testPrefixer to include the browser name in the context here:

const context = { id: %%id%%, title: %%title%%, name: %%name%% };

@yannbf
Copy link
Member

yannbf commented Nov 16, 2022

Hey @vladnicula you can get the browser name by using page.context().browser().browserType().name(). I updated the recipe in our README to reflect that!

@yannbf yannbf closed this as completed Nov 16, 2022
@vladnicula
Copy link
Author

Ups, sorry I missed that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants