Harry
Harry

Reputation: 54969

Playwright debug test to not close browser

I'm using playwright and vscode, especially the "debug test" function which pops up a real browser window to run the test. When it fails the test auto closes, how can I make it not auto close so that I can modify the test and also visually see what the state of the test is?

Upvotes: 9

Views: 3266

Answers (3)

Konstantinas.A
Konstantinas.A

Reputation: 63

If you use VSCode with "Playwright Test for VSCode" extension enabled (https://marketplace.visualstudio.com/items?itemName=ms-playwright.playwright)

you can tick "Show browser" and it will keep browser open.

enter image description here

Upvotes: 1

Don Diego
Don Diego

Reputation: 1508

I think how they managed it is a disaster if compared with cypress. If they would have added a simple option in config files (as cypress does) everything would have been much easier.

Anyway, you have to use one of these solutions, not nice, but working: you have to add at the end of the last function executed one of the following

  • await this.page.waitForTimeout(120000); // will close the browser after 120 seconds
  • await new Promise(() => {}) // will stay open forever until timeout occurs, usually 120 seconds

Upvotes: 2

chiashurb
chiashurb

Reputation: 81

Two things I find helpful in this situation:

  1. Putting a page.pause() immediately BEFORE the failing test will cause the debugger to break there so you can inspect the state of your app;

  2. Playwright's tracing function is quite lovely and will let you see a snapshot of your app at each stage, before/after, including screen images, highlighting failing tests, and lots of other helpful data... so if you weren't expecting a test to fail, or maybe it only fails intermittently, you can use the trace to see the state at the precise moment of failure.

Upvotes: 2

Related Questions