Reputation: 147
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?
Upvotes: 3
Views: 11625
Reputation: 21
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.
Upvotes: 2
Reputation: 4207
You are opening browser once and trying to close after each test , that is the issue.
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
Upvotes: 2