playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] watch mode for e2e tests

Open JoelEinbinder opened this issue 5 years ago • 39 comments

I'd really appreciate a watch mode. My hands hurt from running my test over and over.


If you are looking for the watch mode that runs tests on save, I found assigning the shortcut to Run Test at Cursor very handy: Screenshot 2022-12-01 at 7 44 45 AM

Playwright VS Code Extension is likely to mitigate your request

VS Code extension makes writing and debugging tests convenient, there is no need to go between code and terminal back and forth.

JoelEinbinder avatar Dec 27 '20 07:12 JoelEinbinder

As a workaround with Playwright 1.12+ I’m using the test:e2e:watch npm script below to watch all my test files and run a single test file when it is modified (install chokidar-cli before):

{ "scripts": {
    "test:e2e": "playwright test --config=tests/playwright.config.ts",
    "test:e2e:watch": "chokidar 'tests/e2e/**/*.test.ts' -c 'npm run test:e2e -- --retries=0 {path}'",
}}

Of course modify these scripts according to your needs. Coupled to test.only it’s perfect for my usage.

laurentpayot avatar Jun 10 '21 08:06 laurentpayot

This is going to be rebooted as a part of the debugging story. Closing with no replacement for now.

pavelfeldman avatar Jun 14 '21 23:06 pavelfeldman

As a workaround with Playwright 1.12+ I’m using the test:e2e:watch npm script below to watch all my test files and run a single test file when it is modified (install chokidar-cli before):

{ "scripts": {
    "test:e2e": "playwright test --config=tests/playwright.config.ts",
    "test:e2e:watch": "chokidar 'tests/e2e/**/*.test.ts' -c 'npm run test:e2e -- --retries=0 {path}'",
}}

Of course modify these scripts according to your needs. Coupled to test.only it’s perfect for my usage.

This would be better :) "test:e2e:watch": "chokidar e2e/**/*.{spec,test}.{js,ts} -c 'yarn run test:e2e'"

iamyoki avatar Aug 25 '21 16:08 iamyoki

I made a library playwright-watch to achieve this. See playwright-watch docs

playwright-watch



playwright + watch = palywright-watch

🙈🐵 Run Playwright in watch mode


Usage

use exactly the same as Playwright, only in watch mode

npx playwright-watch test

Note: There is a peer dependency on playwright.

Or you can install locally

yarn add playwright-watch -D

#or

npm install playwright-watch --save-dev

Add script to your package.json

{
  "test:watch": "playwright-watch test"
}

or

{
  "test": "playwright test",
  "test:watch": "playwright-watch exec yarn test"
}

CLI Details

run npx playwright-watch --help to see more details.

🙈 playwright-watch

🐵 Run Playwright in watch mode

Commands:
  playwright-watch <commands..>       Use the same as Playwright but under watch
                                      (e.g. playwright-watch test)     [default]
  playwright-watch exec <commands..>  Run commands when changes are detected
                                      (e.g. playwright-watch exec yarn test)

Options:
  -h, --help     Show help                                             [boolean]
  -v, --version  Show version number                                   [boolean]
  -c, --config   Configuration file of Playwright (default:
                 playwright.config.js), the `testMatch` prop will be watch path
                 (default: `.*(test|spec).(js|ts|mjs)`).

Examples:
  playwright-watch test
  playwright-watch test --config tests/playwright.config.js

  playwright-watch exec yarn playwright test
  playwright-watch exec yarn run test:e2e
  playwright-watch exec echo changed

Author

Yoki Yu (iamyoki)

iamyoki avatar Aug 29 '21 14:08 iamyoki

Thanks for this tool @iamyoki as a workaround!

@pavelfeldman would be great if adding a watch mode could still be considered to be added to Playwright regardless. I’m planning on using PW as a general test runner (mostly for non-web tests), so this functionality would be quite important for my use cases. 🙏

schickling avatar Sep 18 '21 17:09 schickling

@pavelfeldman We really need a watch mode that also leaves the browsers open. I'd be happy to work on this if you could guide me.

YonatanKra avatar Feb 25 '22 08:02 YonatanKra

@pavelfeldman We really need a watch mode that also leaves the browsers open. I'd be happy to work on this if you could guide me.

Yes please!

frederikhors avatar Feb 25 '22 08:02 frederikhors

until then, another workaround using nodemon:

nodemon --watch playwright/specs --exec \"playwright test -c playwright.config.ts\"",

also works great when using with the inspector:

nodemon --watch playwright/specs --exec \"PWDEBUG=1 playwright test -c playwright.config.ts\"",

if using with typescript with custom tsconfig.json, I have set up a pw:dev script in my package.json to watch when tsc changes the output directory to restart playwright:

"pw:dev": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test\"",
"pw:dev:debug": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test:debug\"",
"pw:watch": "tsc --incremental -p playwright/specs/tsconfig.json --watch & tsc-alias -p playwright/specs/tsconfig.json -w",
"pw:test": "playwright test -c playwright.config.ts",
"pw:test:debug": "PWDEBUG=1 playwright test -c playwright.config.ts"

dannyyassine-ce avatar Feb 26 '22 18:02 dannyyassine-ce

until then, another workaround using nodemon:

nodemon --watch playwright/specs --exec \"playwright test -c playwright.config.ts\"",

also works great when using with the inspector:

nodemon --watch playwright/specs --exec \"PWDEBUG=1 playwright test -c playwright.config.ts\"",

if using with typescript with custom tsconfig.json, I have set up a pw:dev script in my package.json to watch when tsc changes the output directory to restart playwright:

"pw:dev": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test\"",
"pw:dev:debug": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test:debug\"",
"pw:watch": "tsc --incremental -p playwright/specs/tsconfig.json --watch & tsc-alias -p playwright/specs/tsconfig.json -w",
"pw:test": "playwright test -c playwright.config.ts",
"pw:test:debug": "PWDEBUG=1 playwright test -c playwright.config.ts"

Before I decided to create playwright-watch, I have tried this way, the only problem is that if the save is triggered multiple times in a short period of time, playwright will concurrently run multiple processes and cause memory overflow.

iamyoki avatar Mar 03 '22 10:03 iamyoki

until then, another workaround using nodemon:

nodemon --watch playwright/specs --exec \"playwright test -c playwright.config.ts\"",

also works great when using with the inspector:

nodemon --watch playwright/specs --exec \"PWDEBUG=1 playwright test -c playwright.config.ts\"",

if using with typescript with custom tsconfig.json, I have set up a pw:dev script in my package.json to watch when tsc changes the output directory to restart playwright:

"pw:dev": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test\"",
"pw:dev:debug": "yarn pw:watch & nodemon --watch playwright/tests-out --exec \"yarn pw:test:debug\"",
"pw:watch": "tsc --incremental -p playwright/specs/tsconfig.json --watch & tsc-alias -p playwright/specs/tsconfig.json -w",
"pw:test": "playwright test -c playwright.config.ts",
"pw:test:debug": "PWDEBUG=1 playwright test -c playwright.config.ts"

Before I decided to create playwright-watch, I have tried this way, the only problem is that if the save is triggered multiple times in a short period of time, playwright will concurrently run multiple processes and cause memory overflow.

you can always add a delay with changing files :) -> https://github.com/remy/nodemon#delaying-restarting

dannyyassine-ce avatar Mar 12 '22 15:03 dannyyassine-ce

We recently started using Playwright where I work & many peer teams are starting to move away from Cypress I don't have much Cypress experience, I started off using Playwright

1 common thing I hear from former Cypress users is if there is something similar to the npm run cy:open command which opens the browser (non-headless) and watches for file changes Simply adding as a data point ^

Resources: If you have feel like Playwright is missing some features which you think are relavant to Cypress, please leave a comment here too so we can help the playwright ecosystem 🙏 : Github Discussion 11201 -- Playwright vs Cypress Feature Comparison - Share Questions & Experience

cliffordfajardo avatar Apr 09 '22 00:04 cliffordfajardo

Hey, adding a bit to the discussion here. About 3 years ago I build a large e2e test suite with Cypress and I was in love with it. After a long time battling with cypress I came to also hate some parts of it and started looking for alternatives and found playwright.

This seems like a winner, but I just can't use it. It's impossible to be productive when writing new specs if I have to constantly type commands go back and forth between the test code and the test runner.

I haven't read all of the documentation yet, but is there something I'm missing? I think watch mode should be one of the things that should just work out of the box.

iamzapata avatar Apr 29 '22 09:04 iamzapata

@pavelfeldman We really need a watch mode that also leaves the browsers open. I'd be happy to work on this if you could guide me.

This is needed, tests should be able to 1.) all run in the same instance of an open browser 2.) reload on test change 3.) reload on dependency change

timcash avatar May 18 '22 20:05 timcash

@iamzapata I think you want this: https://github.com/microsoft/playwright-vscode

pavelfeldman avatar May 27 '22 18:05 pavelfeldman

A watch mode would be great, it's kind of the only deal-breaker between Playwright and Cypress in my opinion.

Is it this issue that's referred to in the docs?: https://playwright.dev/docs/test-components#planned-work

Planned work
Watch mode: watch mode is highly anticipated and should be relatively straightforward in implementation.

franzvezuli avatar Jun 01 '22 23:06 franzvezuli

Ran into this trying to create a VSCode debugger profile for our playwright tests. (I know about https://github.com/microsoft/playwright-vscode (from this thread), but I just wanted to be able to tweak an existing debugger profile because it's faster than learning a whole new tool.)

I started by trying playwright-watch, but couldn't get it work. (The first problem was that our playwright.config.ts is in TS, but even once that was solved, it just hung forever and didn't actually run the test in question.) Eventually got it to work with nodemon. I'll share the results, in case it helps anyone else.

For context:

  • We write in TS, run a monorepo using lerna and yarn workspaces, and use playwright to run our browser-based integration tests.

  • The goal was to replicate the setup we have for our jest tests, wherein the test file currently open in the editor has its tests run in watch mode.

  • The solution here requires a VSCode extension called Tasks Shell Input. That's just to handle the monorepo-i-ness of it all, though, so if that doesn't apply to you, then it (and the entirety of inputs below) can be skipped.

    (P.S. If anyone has a better idea of how to get an arbitrary segment of a path from within a launch.json configuration, I'm all ears, because I recognize this is pretty clunky. I just couldn't figure out any other way (besides the inputs entry below) to get the packageName part of a path like /some/path/here/<repo-name>/packages/<packageName>/test/some.test.ts using just the predefined variables here.)

launch.json

{
  "version": "0.2.0",
  "inputs": [
    // Get the name of the package containing the file in the active tab.
    {
      "id": "getPackageName",
      "type": "command",
      "command": "shellCommand.execute",
      "args": {
        // Get the current file's absolute path, chop off everything up to and including the repo's `packages`
        // directory, the split on `/` and take the first entry
        "command": "echo '${file}' | sed s/'.*sentry-javascript\\/packages\\/'// | grep --extended-regexp --only-matching --max-count 1 '[^\\/]+' | head -1",
        "cwd": "${workspaceFolder}",
        // normally `input` commands bring up a selector for the user, but given that there should only be one
        // choice here, this lets us skip the prompt
        "useSingleResult": true
      }
    }
  ],
  "configurations": [
    // Run a specific test file in watch mode (must have file in currently active tab when hitting the play button).
    // NOTE: If you try to run this and VSCode complains that the command `shellCommand.execute` can't be found, go
    // install the recommended extension Tasks Shell Input.
    {
      "name": "Debug playwright tests (just open file)",
      "type": "pwa-node",
      "cwd": "${workspaceFolder}/packages/${input:getPackageName}",
      "request": "launch",
      "runtimeExecutable": "yarn",
      "runtimeArgs": [
        // `nodemon` is basically `node --watch`
        "nodemon",
        // be default it only watches JS files, so have it watch TS files instead
        "--ext",
        "ts",
        "${workspaceFolder}/node_modules/playwright/node_modules/.bin/playwright",
        "test",
        "${file}"
      ],
      "skipFiles": ["<node_internals>/**"],
      "outFiles": ["${workspaceFolder}/**/*.js", "!**/node_modules/**"],
      "sourceMaps": true,
      "smartStep": true,
      "internalConsoleOptions": "openOnSessionStart",
      // show stdout and stderr output in the debug console
      "outputCapture": "std"
    }
  ]
}

lobsterkatie avatar Jun 14 '22 19:06 lobsterkatie

It would be nice if meanwhile the nodemon solution or similar were added to the documentation :)

bichotll avatar Jul 04 '22 19:07 bichotll

Hi everyone, I just wanted to add some stuff here for nodemon if anyone is looking for a quick refresh that only runs the test you change.

@iamyoki I think your CLI is really cool and I'll probably use it, but just for if someone cannot install more deps or wants to use shell to achieve this, this is the package.json I have:

{
    "scripts": {
         "test:locally:watch": "nodemon --verbose --signal SIGHUP --ext ts --exec \"npm run _test:locally:watch\"",
        "_test:locally:watch": "echo \"Running tests: \n$(find tests -newermt '5 seconds ago')\" && playwright test $(find tests -newermt '5 seconds ago' | tr '\\n' ' ')"
    }
}

Basically, all this does is checks which files changed in the last 5 seconds and runs them, it's not the best solution but it should work in most cases.

kerren avatar Jul 12 '22 10:07 kerren

Please add watch mode to the CLI. I cannot see any reason against it, other than forcing users to vscode. Although I myself use vscode, I don't want to force it upon others. Please Microsoft, be kind and generous.

I want to use playwright for both my e2e tests and unit tests (and for the latter watch mode is a must).

einarpersson avatar Oct 21 '22 06:10 einarpersson

oh wow there is actually no watch mode... huh that was unexpected.

andrewmclagan avatar Nov 10 '22 05:11 andrewmclagan