javascript Playwright -接收错误'locator.click:目标页、上下文或浏览器在测试运行之间已关闭

kokeuurv  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(763)

我正在运行一组针对Chromium的Playwright测试(在Visual Studio Code中)。
当单独运行测试时,它们都通过了。但是一起运行(简单的 npx playwright test),在成功测试后运行的每个测试都会失败,如下所示(作为示例):

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 |

我已经重新排序了测试,只是为了表明它总是在成功的测试之后运行的测试会失败。所以它似乎与测试本身无关。希望这是有意义的。所以在另一个测试中会出现错误(例如):

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

这是使用的userFixture.ts代码

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';

你知道我怎么能找到这个问题吗?

2izufjch

2izufjch1#

你打开浏览器一次,并试图关闭后,每个测试,这是问题。
test.beforeEach
声明在每个测试之前执行的beforeEach挂接。
使用方法:

// 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/');
});

参考:Playwright Test Runner Documentation

相关问题