0

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
dogowar
  • 41
  • 1
  • 8
  • 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](https://playwright.dev/docs/test-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. – ggorlen Apr 01 '23 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? – dogowar Apr 02 '23 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 – dogowar Apr 02 '23 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 [mcve] to be able to answer in more detail than this. – ggorlen Apr 02 '23 at 16:17

1 Answers1

1

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

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
  • 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? – dogowar Apr 02 '23 at 10:36
  • 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 – dogowar Apr 02 '23 at 10:45
  • You may debug the tests to get more info - https://playwright.dev/docs/debug – Vishal Aggarwal Apr 03 '23 at 08:52