Monday, May 12, 2025

Playwright-Cli-Select For Quick Targeted Test Runs via CLI

In this example we will explore how to run targeted tests using CLI with the helps of Playwright-Cli-Select library. Playwright-Cli-Select is an open-source tool designed to enhance the Playwright testing framework by providing an interactive command-line interface (CLI) for selecting and running specific test specs, titles, or tags.

Playwright-Cli-Select For Quick Targeted Test Runs via CLI

Installation

To install the tool, run the following command in your Playwright project directory:

npm install --save-dev playwright-cli-select


Run mode

Then you can run the cli tool with

npx playwright-cli-select run
OR
npx playwright-cli-select run --project=chromium
OR
npx playwright-cli-select run --project=chromium --headed


Playwright-Cli-Select For Quick Targeted Test Runs via CLI

This provides a ton of easy to follow examples on how you can customize your run command. All of this can easily be added to your package.json script in order to have a simple npm run cli command, below I've created so the command automatically selects the first specs option for me.

package.json

{
  "scripts": {
    "cli": "npx playwright-cli-select run --specs",
    "test": "npx playwright test"
  }
}

Once you are in the cli interface, you will need to use Tab as the "select" key. This was a small learning curve as by default I was just trying to press Enter and that actually allows you to "Proceed" Thankfully I read the interface and there were clear instructions on what the buttons do!

If you ever do get stuck or want to explore the tool without reading the docs on GitHub you also have access to npx playwright-cli-select run --help a help flag which providers all the different options at your fingertips.


Additionally, If you want to skip straight to selecting specs, titles and/or tags:

npx playwright-cli-select run --specs
# skips straight to spec selection

npx playwright-cli-select run --titles
# skips to test title selection

npx playwright-cli-select run --tags
# skips to tag selection

npx playwright-cli-select run --titles --tags
# skips to test title selection, followed by tag selection
# Any combination of `--specs`, `--titles` and/or `--tags` parameters is permitted.

Command line arguments

You can also include more cli arguments similar to npx playwright test:

npx playwright-cli-select run --project firefox webkit


Using last failed

Passing Playwright's --last-failed parameter filters the available selections in each prompt to the last failed tests. Using this package, you can further drill down on specific tests within the last failed tests:

npx playwright-cli-select run --last-failed


Using only changed

Passing Playwright's --only-changed parameter filters available selections to the changed tests detected. To specify a specific subset of changed tests rather than running entire changed test files:

npx playwright-cli-select run --only-changed

UI mode

You can append the --ui or --headed parameters to open a browser and view selected specs, test titles and/or tags:

npx playwright-cli-select run --ui

OR

npx playwright-cli-select run --titles --headed
# Example of choosing only test titles to run headed

This is all about interactive cli commands to run targeted tests in playwright.


No comments:

Post a Comment