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

[Feature Request] Check browser in play function #379

Open
ashleyryan opened this issue Nov 8, 2023 · 2 comments
Open

[Feature Request] Check browser in play function #379

ashleyryan opened this issue Nov 8, 2023 · 2 comments
Labels
feature request New feature or request

Comments

@ashleyryan
Copy link

ashleyryan commented Nov 8, 2023

Context
We use test-runner to run tests using playwright against chromium and webkit. There are some tests that I'd like to skip in webkit, if possible.

In the preRender function in test-runner config, I can access the browser name from playwright using page.context()?.browser()?.browserType().name(), but as far as I can tell, the page object is not accessible within a play function's context, nor is it possible to mutate the context object inside of preRender to add browser data to it to check in the play function.

Is it possible to determine which browser the test is running in from within a play function? Or is it possible to access the page object at all?

@yannbf
Copy link
Member

yannbf commented Nov 10, 2023

Hey @ashleyryan! Thanks for writing this issue. Unfortunately there's no equivalent of Playwright's conditional test annotations in the test-runner.

As a temporary workaround, the play function runs in the browser, so you could create a small utility or just add an inline check based on browser detection, then do an early return so it won't do anything when executing in that browser. You can combine with Test-runner's user agent so this will only happen when the story is accessed via the test-runner:

function shouldSkipTest() {
    const UA = window.navigator.userAgent;
    const isTestRunner = UA.match(/StorybookTestRunner/);
    const isWebkit = /\b(iPad|iPhone|iPod)\b/.test(UA) && /WebKit/.test(UA) && !/Edge/.test(UA) && !window.MSStream;
    return isTestRunner && isWebkit;
}

export const MyStory = {
  play: async ({canvasElement}) => {
    if(shouldSkipTest) return;

    // the actual tests here...
  },
};

@yannbf yannbf added the feature request New feature or request label Nov 10, 2023
@ashleyryan
Copy link
Author

Oh excellent, I was concerned about this checking for safari in the Interactions tab, which I don't want, so thank you for the check for the TestRunner UA. I'll give that a try

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

No branches or pull requests

2 participants