Playwright - What's the difference between 'playwright test' within 'package.json' and 'npx playwright test'?

Viewed 473

I'm just getting started with Playwright but I'm getting confused between the cli commands used to run tests, e.g. npx playwright test and commands that you add to your package.json, for example...

{
  "scripts": {
    "pretest": "tsc --incremental -p tests/tsconfig.json",
    "test": "playwright test -c tests-out"
  }
}

I'm aware that I can run npm test here, but why does this work within a script but not when I simply run playwright test -c tests-out in the terminal? I just get Unknown command: "playwright".

What is the association between the npx playwright test command and playwright test?

Also why does -c refer to an output directory here yet for npx playwright test this refers to a configuration file?

The reason I ask is that in a typescript Playwright project I am working on the following scripts are defined...

"e2e-playwright-build": "cd apps/playwright-e2e && tsc",
"e2e-playwright": "npm run e2e-playwright-build && cd apps/playwright-e2e/src/tests && playwright test -c tests-out --config=../../tests-out/apps/playwright-e2e/src/tests/configs/playwright.config.js --grep-invert \"@example|@visual|@access|@failfast\"",

These commands are too broad for my requirements and I would like to run specific tests based on my own commands, however whenever I try to run npx playwright test I just get...

Unexpected current working directory - C:\Tribal\tribal.edge.ui\module\tribal.edge.ui\apps\playwright-e2e\tests-out

With a bunch of other errors. My folder structure looks like the following...

├── apps
│   ├── playwright-e2e
│      ├── src
├── node_modules
├── package.json
├── package-lock.json 
└── .gitignore
2 Answers

First thing first. If you are not passing your config file location by default it will try to search for config file in the current root directory.

When you are trying to run tests from terminal with "npx playwright test" you need to be in appropriate folder. Root of that folder must contain "playwright.config.ts" or "playwright.config.js" depending on are you compiling to .js (and I see you do by "tsc").And also .spec.ts files need to be in subfolders.

From documentation -c or --config : Configuration file. If not passed, defaults to playwright.config.ts or playwright.config.js in the current directory.

See more on: https://playwright.dev/docs/test-cli

while running the test case files are insides some folder then you need to run as below and don't provide a config file (-c option) while running. It should work I hope.

Command to run

npx playwright test tests/supportpanel

enter image description here

Related