Marius Krenevičius
Marius Krenevičius

Reputation: 37

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?

Upvotes: 2

Views: 22637

Answers (2)

aoki ken
aoki ken

Reputation: 136

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

Upvotes: 1

Ganesh H
Ganesh H

Reputation: 1787

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

Upvotes: -1

Related Questions