You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright中如何实现测试套件并行但套件内测试串行执行

解决方案

Playwright原生支持你需要的执行逻辑,无需调整现有实现思路,按以下两步配置即可:

  • 第一步:开启单文件内测试套件并行能力
    在playwright.config.ts配置文件中添加fullyParallel: true配置,该配置会允许单个测试文件内的多个独立测试套件并行执行,刚好匹配你不同catalog测试套件无依赖、可并行的要求。
  • 第二步:指定单个测试套件内用例串行执行
    在你动态生成的每个test.describe回调内部的开头位置,添加test.describe.configure({ mode: 'serial' })配置,即可强制当前套件下的所有测试用例按顺序串行执行,完全适配你复用Page实例、利用客户端缓存提升执行效率的需求。

修改后的代码示例

catalogs.forEach((objectIdsOfCatalog, catalogId) => {
    test.describe('Test catalog "' + catalogId + '"', () => {
        // 新增这一行即可实现套件内用例串行
        test.describe.configure({ mode: 'serial' });

        let newVersion: PageObject;
        let actualVersion: PageObject;

        test.beforeAll(async ({browser}) => {
            console.log('New page for', catalogId);
            const {actualUrl, newUrl} = getConfig();
            const context = await browser.newContext();
            actualVersion = new PageObject(await context.newPage(), actualUrl);
            newVersion = new PageObject(await context.newPage(), newUrl);
        });

        test.afterAll(async () => {
            console.log('Close page for', catalogId);
            actualVersion.close();
            newVersion.close();
            actualVersion = null;
            newVersion = null;
        });

        for (const objectId of objectIdsOfCatalog) {
            test('Testing "' + objectId + '"', async () => {
                // 测试逻辑
            });
        }
    });
});

补充说明

串行模式下如果前序测试用例执行失败,后续关联用例默认会跳过执行,如果你需要失败后仍继续执行所有用例,可以单独给测试套件配置failFast: false。

内容的提问来源于stack exchange,提问作者stephan.niedermayr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:45:04