13

Is it possible to run multiple tests in one browser window for Playwright/test?

Currently it will hit browser.close(); after every test even though they are testing on the same page which puts a lot of extra time on the tests.

test.beforeAll(async ({ browser }) => {
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.com');
});

test('nav test', async ({ page }) => {
  const name = await page.innerText('.navbar__title');
  expect(name).toBe('Playwright');
});

test('header test', async ({ page }) => {
  const name = await page.innerText('.navbar__header');
  expect(name).toBe('Playwright');
});
2
  • Related: Run grouped tests sequentially using Playwright Commented Aug 25 at 15:48
  • The title is partly incomprehensible. The OP is gone ("Last seen more than 4 years ago"); can someone fix or propose a fix? Commented Oct 15 at 21:32

2 Answers 2

27

When you create a test like this, test('header test', async ({page}) => {, you're specifying page and telling it to create a new page context.

Remove the page from the test, and share the one you create from your .beforeAll.

Try this:

test.describe('1 page multiple tests', () => {
    let page;
    test.beforeAll(async ({ browser }) => {
        const context = await browser.newContext();
        page = await context.newPage();
        await page.goto('https://example.com');
    });

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

    test('nav test', async () => {
        const name = await page.innerText('h1');
        expect(name).toContain('Example');
    });

    test('header test', async () => {
        const name = await page.innerText('h1');
        expect(name).toContain('Domain');
    });
});

Run it like this:

npx playwright test .\StackTests_SinglePage.spec.ts --headed

(you can see the name of my file in there)

You might need to toggle it down to one worker if it tries to parallel run your test.

For me, that code opens one browser, one page, passes both tests, and then closes out.

Enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

If you are using playwright.config.ts "playwright.dev/docs/test-configuration#emulation" Does this work passing the newContext to test on different browsers?
The same code above works fine cross browser. Playwright isolates all these fixtures by design. That's why sharing the page in the describe keeps the same context. If you have a more detailed question around your configuration you might want to create a new question. It's a different one than you've asked here and it means other people can support you ... if the above answers what you asked it's good to mark it as the right answer so others searching around the same problem can learn :-)
browser.close; is a no-op. You'd need to call it: await browser.close(); for it to have any effect
I tried my tests like above.. but still each test is getting executed in new browser. can anyone suggest if required any changes in playwright.config.ts file any specific?
Please provide the program output as text, not as an image. Thanks in advance. See also Why should I not upload images of code/data/errors?
0

Can you try wrapping the tests in a describe block? So they are treated as a group and not as an individual tests.

test.describe('two tests for same page', () => {
   test('nav test', async ({ page }) => {    
     const name = await page.innerText('.navbar__title');  
     expect(name).toBe('Playwright');
   });

   test('header test', async ({ page }) => {   
    const name = await page.innerText('.navbar__header');  
    expect(name).toBe('Playwright');
  });
});

1 Comment

Wrapping still seems to open a new browser each time, Id like to keep it open and use it then call a browser close in test.afterAll

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.