3

I am running a set of Playwright tests against Chromium (in Visual Studio Code).

When running the tests individually, they all pass. However running together (simple npx playwright test), each test that runs after a successful test, will fail with the following (as an example):

frame.setInputFiles: Target page, context or browser has been closed

  3 | test('User can play video files - mp4 @FullRegression', async ({ page }) => {
  4 |   //Upload mp4 video file
> 5 |   await page.setInputFiles('input[type="file"]', [
    |   ^
  6 |     '../webapp/tests/TestUploadFiles/20220513_094253.mp4'
  7 |   ])
  8 |

I have re-ordered the tests, just to show that it is always the test that runs AFTER a successful test that will fail. So it does not appear to be anything to do with the test itself. Hopefully that makes sense. So will error on another test as (for example):

locator.click: Target page, context or browser has been closed

This is the userFixture.ts code used

import { test as base, Page, BrowserContext } from '@playwright/test';

//Only calls beforeAll and afterAll once per worker
base.describe.configure({ mode: 'serial' });

let context: BrowserContext
let page: Page
const baseUrl = process.env.CI ? process.env.TEST_URL : '/'

base.beforeAll(async ({ browser }) => {
  context = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
  page = await context.newPage();
  await page.goto(baseUrl!);
  await page.waitForLoadState("networkidle")
  const user_another_account = await page.$("text='Pick an account'");
  if(user_another_account)
  {
    await page.getByRole('button').first().click();
  }
})

//Override default request context with our api version
export const test = base.extend({
    page: async ({ }, use) => {
        await use(page);
    },
});

base.afterEach(async ({ browser} ) => {
  await browser.close();
});

export { expect } from '@playwright/test';

Any idea how I can find the issue here?

4
  • 1
    Are you writing tests that depend on a specific ordering? I don't see any tests other than the snippet in your error message in your example. Ideally, though, every test is idempotent, fully set up and torn down, so that tests can run in parallel or any ordering. Basically, use beforeEach. I'd remove await page.waitForLoadState("networkidle") --goto already waits for a load state, so I'd pass {waitUntil: "networkidle"} as an argument to that. Commented Apr 1, 2023 at 17:49
  • Thanks @ggorlen, you are right, should have been beforeEach. I have done that however issue still persists. Thanks for the feedback on the goto option also. As per my below comments, do you know if there is a way to debug and find what is actually triggering the close? Commented Apr 2, 2023 at 10:38
  • Tests are not specific ordering. Also noting the test passes every time when run individually, it is only when it is part of a test run this occurs. So it seems something else is triggering the close, not the test itself. That's what makes it hard to find out what is causing the issue ;p Commented Apr 2, 2023 at 10:44
  • When tests work individually, but fail as a group, then they're not idempotent. Eliminate any shared state so that each test is fully isolated from the rest. I'd have to see a minimal reproducible example to be able to answer in more detail than this. Commented Apr 2, 2023 at 16:17

2 Answers 2

3

In my case, I was calling an async function that was doing the work, but I forgot to await the call in my .spec file, so the test was terminating by the time it was trying to do the actual work.

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

1 Comment

Give this man a knighthood. This is helped for page.evaluate(() => {}) errors where there were no further actions after the call.
2

You are opening browser once and trying to close after each test , that is the issue.

test.beforeEach

Declares a beforeEach hook that is executed before each test.

Usage:

// example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
  console.log(`Running ${testInfo.title}`);
  await page.goto('https://my.start.url/');
});

test('my test', async ({ page }) => {
  expect(page.url()).toBe('https://my.start.url/');
});

Reference: Playwright Test Runner Documentation

3 Comments

Thanks @vishal. You are right, should have been beforeEach. I have done that however issue still persists. It is difficult to track down the issue, as using tracing does not provide the point browser was closed. Is there a debugging mechanism that you could share if known, to show what is actually triggering the close?
Also noting the test passes every time when run individually, it is only when it is part of a test run this occurs. So it seems something else is triggering the close, not the test itself. That's what makes it hard to find out what is causing the issue ;p
You may debug the tests to get more info - playwright.dev/docs/debug

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.