Konzy262
Konzy262

Reputation: 3087

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

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

Upvotes: 5

Views: 5827

Answers (5)

Vishal Aggarwal
Vishal Aggarwal

Reputation: 4171

npx allows you to directly execute Javascript packages without installing them.

When inside a dev project directory , playwright is installed as package dependency through package.json then its available to execute without using npx as its installed.

Whereas when we install and use playwright as a standalone set up , we run it through npx command.

In general when somebody will use npx over npm???

No global installation: npx allows you to run packages without installing them globally. This can help keep your workspace organized and ensure you're using the latest version of a package.

Run different versions: npx allows you to run different package versions without affecting the installed version.

Run locally installed packages: npx automatically resolves and runs locally installed package binaries.

Saves disk space : npx is a more flexible solution that enables you to execute packages without installing them globally. This approach saves disk space and helps keep your global variables organized.

Test packages: npx is perfect for testing packages before integrating them into your codebase.

Bootstrap new projects: npx saves you time when bootstrapping new projects.

Run js scripts: With npx, you can run js scripts directly from a gist.

Reference: https://www.codingninjas.com/studio/library/difference-between-npm-and-npx

Upvotes: 0

code_akash
code_akash

Reputation: 41

In order to use native Playwright test command:

  1. Keep your config file at project root level. If you have multiple config files, you will need to use --config or -c option with test command to specify a config file.

    Example: npx playwright test --config playwright_cloud.config.ts

  2. Now, Define the test directory path, using testDir option in your config file(s). You can still pass a particular spec file path on command line (Point 4)

enter image description here

  1. Once you have your test directory defined in config file and the config file is at root level, the test command, by default, will pick this Playwright config file(unless there are multiple files). All the spec files at the testDir path will be scanned for execution.

  2. To run a specific spec file or a folder, you can still use the path to this file or folder in the test command. Example: For running a particular file npx playwright test src/e2e/sample.spec.ts or for a folder with multiple files: npx playwright test src/e2e/

Recommended: Create alias of these command on your machine. Will save a lot of time and no fear to forget them.

Upvotes: 1

Shashank KVS
Shashank KVS

Reputation: 1

NPM is used as a package manager, NPX, on the other hand, is used to execute Javascript packages.

Upvotes: -1

yuvraj
yuvraj

Reputation: 197

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

Upvotes: -1

Gaj Julije
Gaj Julije

Reputation: 2163

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

Upvotes: 1

Related Questions