Rayrun
← Back to Discord Forum

I want to reuse same page for my websites Login, run all the tests inside the describe block, logout

I want to reuse same page for my websites Login, run all the tests inside the describe block and logout. i am using a beforeAll and afterAll hook.

test.describe("demo", () => { let browser: Browser; let context: Context; let page: Page;

test.beforeAll(async ({ browser }) => { context = await browser.newContext(); page = await browser.newPage(); await Login({ page, browser }); });

test.afterAll(async () => { await Logout({ page }); await browser.close(); });

test("demo", async ({ page }) => { let delay = 10;

const coordinates = [
  { x: 195, y: 152 },
  { x: 622, y: 150 },
  { x: 618, y: 417 },
  { x: 193, y: 392 },
  { x: 197, y: 150 },
  { x: 626, y: 286 },
  { x: 916, y: 284 },
  { x: 910, y: 550 },
  { x: 472, y: 543 },
  { x: 473, y: 418 },
];

// handling all the clicks on page using for loop in function below
await ClickCoords(page, coordinates, delay);

// calculating start and end for drag operations

const { start, end } = dragCoords(coordinates);

await page.getByRole("img", { name: "pointer" }).click();

// Now you can use these start and end points in your dragMouse function

let mesh = await getBrep(page, start, end);

expect(mesh).toBeDefined();

)} )}

The login and logout functions have the navigations to my page and login, then the test should run on the same page. But right after login, playwright opens a new about:blank page and tries to run the test within that page. which obviously fails. Im not able to figure out why it is opening a new page for the tests. I want it to run in the logged in page opnened in beforeAll hook.

This thread is trying to answer question "Why is playwright opening a new page for the tests after login, and how can tests be run on the same page without going through a login and logout cycle for each test?"

3 replies

As I see, you use page from context, instead you should use the page you created.

Replace:

test("demo", async ({ page }) => {

By:

test("demo", async () => {

ohh, lemme try

okay when i run like this, the tests are running, but they are still being run like beforeEach, the tests run one after the other in diffrent pages, all going through a login and logout cycle. which is what i was trying to avoid

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].