Playwright 测试运行器:将测试限制在一个具有多个项目的配置文件的浏览器上

Playwright test runner: Limit test to only one browser with a config file that has many projects

我有一个包含许多项目的测试配置文件 (playwright.config.ts)。

通常,我想测试几个不同的浏览器。

但是,当我构建测试时,我只想用一个 browser/project 进行测试(因为在我进行测试时测试会失败)。

我试过这个命令:

npx playwright test test.ts --browser='chromium'

但是我得到一个错误:

Error: Cannot use --browser option when configuration file defines projects. Specify browserName in the projects instead.

如何“在项目中指定 browserName”以在一个浏览器上将测试限制为 运行?

我的配置文件:

const config: PlaywrightTestConfig = {
  projects: [
    {
      name: 'Chrome Stable',
      use: {
        browserName: 'chromium',
        channel: 'chrome',
      },
    },
    {
      name: 'Safari MacBook Air',
      use: {
        browserName: 'webkit',
        viewport: {
          width: 2560,
          height: 1620,
        },
      },
    },
    {
      name: 'Firefox Desktop',
      use: {
        browserName: 'firefox',
        viewport: {
          width: 1920,
          height: 1080,
        },
      },
    },
    {
      name: 'iPhone 6/7/8',
      use: devices['iPhone 8'],
    },
    {
      name: 'iPhone 6/7/8 Plus',
      use: devices['iPhone 8 Plus'],
    },
    {
      name: 'iPhone 12',
      use: devices['iPhone 12'],
    },
    {
      name: 'iPhone 12 Pro',
      use: devices['iPhone 12 Pro'],
    },
    {
      name: 'iPhone 12 Pro Max',
      use: devices['iPhone 12 Pro Max'],
    },
    {
      name: 'iPhone 5/SE',
      use: devices['iPhone SE'],
    },
    {
      name: 'iPad',
      use: devices['iPad (gen 7)'],
    },
    {
      name: 'iPad landscape',
      use: devices['iPad (gen 7) landscape'],
    },
    {
      name: 'iPad Mini',
      use: devices['iPad Mini'],
    },
    {
      name: 'iPad Mini landscape',
      use: devices['iPad Mini landscape'],
    },
    {
      name: 'iPad Pro 11',
      use: devices['iPad Pro 11'],
    },
    {
      name: 'iPad Pro 11 landscape',
      use: devices['iPad Pro 11 landscape'],
    },

当使用像您这样的项目时,您可以使用 CLI 参数 --project-p 来过滤它们。例如:

npx playwright test test.ts --project='Chrome Stable'

有关详细信息,请参阅 here

您也可以将此注释掉以 运行 您的测试仅针对 chromium。

{ 姓名:'Safari MacBook Air', 采用: { 浏览器名称:'webkit', 视口:{ 宽度:2560, 身高:1620, }, }, }, { 姓名:'Firefox Desktop', 采用: { 浏览器名称:'firefox', 视口:{ 宽度:1920, 身高:1080, }, }, },