playwright icon indicating copy to clipboard operation
playwright copied to clipboard

Add ability to use AND and OR expression via grep from command line

Open humbienri opened this issue 4 years ago • 20 comments

To bring Windows usage up to par with usage on Mac OS X or Linux, npx playwright test -g "tag1|tag2"

should also work from the Windows command line. Please.


Edit by maintainers: Here are Git Bash and PowerShell solutions:

PowerShell:

npx playwright test --grep  "`"@test1|@test2"`"

Git Bash

 npx playwright test --grep "@test1^|@test2"

humbienri avatar Oct 20 '21 22:10 humbienri

@humbienri : on Windows do this:

npx playwright test -g "tag1^|tag2"

pavelfeldman avatar Oct 21 '21 00:10 pavelfeldman

@pavelfeldman I tried the fix on new project but it doesn't seem to be working for me

I'm on windows 10

powershell: doesn't work image

CMD: works with just -g tag1|tag2 image

Git Bash: works with just -g tag1|tag2 image

ychaudhari avatar Oct 21 '21 19:10 ychaudhari

and how can I adapt to predefined sript (in package.json) "scripts": { "onchrome":"playwright test tests/smoke/smokeTests.spec.ts --config=playwright.config.ts --project=chrome" }

so it is working but i would like to use with scriptcommand e.g. npm run onchrome -g "list|response"

dongokovacs avatar Apr 07 '22 13:04 dongokovacs

how to use and condition in grep?

-g "xy&yz" is not working

jatin-karla11 avatar May 05 '22 18:05 jatin-karla11

how to use and condition in grep?

-g "xy&yz" is not working

Grep is just matching the test title, so you could do something like -g "xy.+yz"

timtait avatar May 20 '22 08:05 timtait

I also get 'commission' is not recognized as an internal or external command, operable program or batch file.

my full command on Powershell: npx playwright test -g "register|commission" --config=playwright.config.js --workers=3 --retries=1

P.S.: tried escaping it, but still didn't help

btadros avatar Jul 07 '22 15:07 btadros

In Powershell the stop-parsing token helps:
npx playwright test -g --% "tag1|tag2"

i-savitski avatar Aug 04 '22 08:08 i-savitski

Could someone execute this from the package.json script ?

I m trying: "test:textbox": "npx playwright test --config=./configs/playwright-web.config.js --project=chromium --grep text1 | text2" "test:textbox": "npx playwright test --config=./configs/playwright-web.config.js --project=chromium -g text1|text2"

And also using the stop-parsing token mentioned above. Always getting the same kind of error

"'text2' is not recognized as an internal or external command"

imartinflores avatar Oct 04 '22 14:10 imartinflores

p-parsing token mentioned above. Always getting the same kind of erro

Still facing the issue.

shashankkvs309668 avatar Oct 25 '22 10:10 shashankkvs309668

@imartinflores - same here...

"test:form": "npx playwright test --project=chromium -g @payx|@ezlm"

GustavoBernardi avatar Nov 03 '22 11:11 GustavoBernardi

The issue in pwsh is how the command is interpreted! One workaround is by starting the npm process manually and passing the arguments split by pipe as it is used on bash:

$npxArgs = "playwright", "test", "-g", "tag1|tag2|tag3"
Start-Process npx -ArgumentList $npxArgs -Wait -NoNewWindow -ErrorAction Stop

mihalqesko avatar Nov 16 '22 19:11 mihalqesko

how to use and condition in grep? -g "xy&yz" is not working

Grep is just matching the test title, so you could do something like -g "xy.+yz"

  • This has really worked in Windows / VSCode powershll.
  • -g "xy.+yz" is a And condition

skumar09 avatar Mar 25 '23 07:03 skumar09

Could someone execute this from the package.json script ?

I m trying: "test:textbox": "npx playwright test --config=./configs/playwright-web.config.js --project=chromium --grep text1 | text2" "test:textbox": "npx playwright test --config=./configs/playwright-web.config.js --project=chromium -g text1|text2"

And also using the stop-parsing token mentioned above. Always getting the same kind of error

"'text2' is not recognized as an internal or external command"

i tried this way with grep under projects in playwright.config.js, and it is working for me, grep:/^(?=.\bsmoke\b)(?=.\bregression\b).*$/m,

This is will read your test title and perform regex on it

shashankkvs309668 avatar Mar 28 '23 06:03 shashankkvs309668

npx playwright test -g "tag1^|tag2"

This clearly doesn't work on Windows and many people have already mentioned this, however, it is still being replied as answer to multiple threads.

Any updates on this?

irharrier2 avatar Jun 08 '23 08:06 irharrier2

Is there any way to --grep based on three annotations? Condition: Run test that contain @API or @UI on @PROD

hsuraweera avatar Jul 10 '23 08:07 hsuraweera

Escaping a pipe symbol in PowerShell is unfortunately not straightforward. Escaping it in Git Bash and PowerShell is both different:

PowerShell:

npx playwright test --grep "`"@slow|@fast"`"

Git Bash

 npx playwright test --grep "@slow^|@fast"

Unfortunately this is not something we can fix on our side (since the "magic" happens inside the terminals), but what we can do is update our docs to include these two workarounds.

mxschmitt avatar Jul 19 '23 10:07 mxschmitt

Here are tested solutions (W11 in VS Code) :

Logical OR for tags

PowerShell: npx playwright test --grep --% "@fast^|@slow"

Git Bash: npx playwright test -g "@fast|@slow"

CMD: npx playwright test --grep "@fast^|@slow"

For npm script in package.json: "npx playwright test -g \"@fast|@slow\""

Logical AND for tags works in all terminals

npx playwright test --grep "(?=.*@fast)(?=.*@slow)"

Remember to check console where script is ran:

obraz

jaktestowac avatar Jul 19 '23 13:07 jaktestowac

@mxschmitt have look on PR with docs update https://github.com/microsoft/playwright/pull/24304

jaktestowac avatar Jul 21 '23 20:07 jaktestowac

What's the proposed approach when you want to combine some rules from npm script and interactive command line? Example (suggested by @ffluk3):

{
  "scripts": {
    "e2e": "playwright --grep-invert \"@wip\""
  }
}

However, if I wanted to omit another tag on the fly, I wouldn't be able to run npm run e2e -- --grep-invert @flaky. Is there a recommended way to approach this use case?

vojta-bisek avatar Aug 31 '23 17:08 vojta-bisek

In my Windows terminal I'm able to get the OR grep to work by adding a space around the pipe: npx playwright test -g "@fast | @slow"

ZachMcCorkhill avatar Jan 25 '24 19:01 ZachMcCorkhill

Different shells require different escaping, we updated our documentation recently to reflect these changes: https://playwright.dev/docs/test-annotations#tag-tests

Please re-file if you have concrete filters which don't work. Please let us know the following:

  • Which shell you are using
  • How your test is named
  • What your expectation is

Closing for now since we addressed it.

mxschmitt avatar Feb 12 '24 14:02 mxschmitt