简体   繁体   中英

How to reuse the same page context in different tests Playwright (javascript)

I have been writing various tests using Playwright, importing them to a separate file and executing from there. My aim is to execute the tests in a specified order using a single worker.

A series of different tests that launch in a specified order

1

What I don't like is that Playwright launches a new browser context for each test. I found a possible solution on DZone which explains that if you don't pass the page object to each test and only declare it in the beforeAll function then the tests use the same context. Unfortunately I can't replicate that example because each of my tests is in a different file and exported as a function (example in the image below).

Test example

3

I've been trying to solve this by declaring the page object in a beforeAll test or before the function that contains one of the tests or import it from a different file and use it as a function parameter but none of the solutions seem to work. Adding a failing example:

Failing example

4

Is there a way to declare the page object before the test function or import it from a different file using JavaScript and use it as a function parameter?

Playwright offers global setup and teardown , Create the page instance inside your global setup and you can use it anywhere inside your tests.

Make a state.json in globalsetup and reusing it for each test.

https://playwright.dev/docs/test-auth

or Reuse the signed in page in multiple tests

https://playwright.dev/docs/test-auth#reuse-the-signed-in-page-in-multiple-tests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM