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?
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?
Share Improve this question edited Apr 1, 2023 at 17:42 ggorlen 57.5k8 gold badges112 silver badges154 bronze badges asked Apr 1, 2023 at 7:14 dogowardogowar 1472 gold badges3 silver badges11 bronze badges 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 removeawait page.waitForLoadState("networkidle")
--goto
already waits for a load state, so I'd pass{waitUntil: "networkidle"}
as an argument to that. – ggorlen 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 ments, do you know if there is a way to debug and find what is actually triggering the close? – dogowar 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 – dogowar 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. – ggorlen Commented Apr 2, 2023 at 16:17
2 Answers
Reset to default 2You 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
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.