1

I have many playwright test cases, I want some of the selected test cases to only run on Chrome. How can that be achieved?

Example:

 test('test 1', async () => {
   
  });

 test('test 2', async () => {
   
  });

 test('test 3', async () => {
   
  });

Now I want the test 1 case to run only on Chrome and the rest of them by default should run on every browser.

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
ali
  • 492
  • 2
  • 7
  • 18
  • Is there any specific reason to only run test case 1 on Chrome? – Lain Feb 27 '23 at 14:40
  • yes, there is the reason we have test cases of lighthouse performances checks where we found out that lighthouse only runs on chrome not on firefox and etc. so by default all other test cases are running on all other browsers and on the CI pipeline for firefox it fails as a lighthouse doesn't support firefox, so we need to run it on chrome. – ali Feb 27 '23 at 14:54
  • [I actually added an answer to another similar question](https://stackoverflow.com/a/75419941/10360471), but to clarify, are those tests in the same file as other tests? Or are they in their own files or even folders? Basically, could you clarify the structure and/or separation of the tests? This would help with giving a more targeted answer. – David R Mar 01 '23 at 06:19

3 Answers3

3

I expect there is a better way to do this with some config magic, but you could conditionally skip based on the browser:

test('Hello World', async ({ browserName, page }) => {
    
    test.skip(browserName.toLowerCase() !== 'chromium', 
    `Test only for chromium!`);
    
    // rest of test
});
AJG
  • 476
  • 2
  • 5
0

Using Tags with 'grep' command at command line:

# This will run @chromeOnly
$ npx playwright test --grep @chromeOnly

# This will run @allBrowsers
$ npx playwright test --grep @allBrowsers

Playwright Commandline

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
-1

Adding Tags to Test Descriptions

In Playwright, you can add tags to your test descriptions using the @tag syntax. While you can technically use any string as a tag, it's recommended to stick with the @tag convention for consistency. Here's an example of how to add tags to a test description:

test('user can login @smoke @login', async ({ page }) => {
  // Test implementation goes here
});

Running Tests with Specific Tags

Playwright provides the --grep and --grep-invert command-line flags to run tests based on their tags. The --grep flag allows you to run tests that match a specific tag pattern, while --grep-invert lets you exclude tests that match the pattern. Here are some examples of how to run tests with specific tags:

# Run tests with the @smoke tag
npx playwright test --grep "@smoke"

# Run tests with the @login tag, excluding those with the @smoke tag
npx playwright test --grep "@login" --grep-invert "@smoke"

Combining Tags for Complex Test Selection

In addition to using single tags, you can also combine multiple tags to create more complex test selection criteria. Here are some examples of how to do this:

# Run tests with either the @smoke or @login tag (logical OR)
npx playwright test --grep "@smoke|@login"

# Run tests with both the @smoke and @login tags (logical AND)
npx playwright test --grep "(?=.*@smoke)(?=.*@login)"

These examples are taken from Organizing Playwright Tests using Tags

Luc Gagan
  • 301
  • 9