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

运行npx playwright test是否存在配置缺失?Playwright Axe测试异常求助

问题描述

在ReactJS Web应用上进行Playwright Axe网页可访问性测试,现有测试代码及项目配置如下。执行npx playwright axe时会运行App.test.ts,但指定路径执行测试时显示Running 0 tests using 0 workers,咨询运行npx playwright test是否存在配置缺失问题。

项目结构

Client -
   - node_modules
   - src
   - a11ytests
     - a11y.specs.ts
   - playwright.config.js
   - package.json

测试代码(a11y.specs.ts)

import { chromium, Browser, Page } from "playwright";
import { test } from "@playwright/test";
import { injectAxe, checkA11y } from "axe-playwright";

let browser: Browser;
let page: Page;

test.describe("Playwright web page accessibility test", () => {
  test.beforeAll(async () => {
    browser = await chromium.launch();
    page = await browser.newPage();
    await page.goto('https://www.w3.org/WAI/demos/after/before/news.html');
    await injectAxe(page);
  });

  test("simple accessibility run", async () => {
    await checkA11y(page);
  });
});

配置文件(playwright.config.js)

const config = {
  timeout: 30000,
  globalTimeout: 600000,
  reporter: "list",
  testDir: "./a11ytests",
};

module.exports = config;
问题分析与解决

1. 测试文件匹配规则不匹配

Playwright默认仅识别后缀为*.spec.ts或*.test.ts的测试文件,你的测试文件命名为a11y.specs.ts(带s),不在默认匹配范围内,导致Playwright找不到测试文件。

解决方法二选一:

  • 重命名测试文件为a11y.spec.ts(去掉后缀的s)
  • 在playwright.config.js中添加testMatch配置,自定义匹配规则:
    const config = {
      timeout: 30000,
      globalTimeout: 600000,
      reporter: "list",
      testDir: "./a11ytests",
      testMatch: ["*.specs.ts"] // 匹配你的测试文件后缀
    };
    module.exports = config;
    

2. 测试代码规范优化(非必填,推荐执行)

手动管理browser和page实例不符合Playwright Test最佳实践,易出现资源泄漏问题,建议改用内置fixture自动管理浏览器实例:

import { test } from "@playwright/test";
import { injectAxe, checkA11y } from "axe-playwright";

test.describe("Playwright web page accessibility test", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('https://www.w3.org/WAI/demos/after/before/news.html');
    await injectAxe(page);
  });

  test("simple accessibility run", async ({ page }) => {
    await checkA11y(page);
  });
});

3. 确认命令执行路径

确保在Client目录下执行npx playwright test命令,若需指定单个测试文件,可使用:

npx playwright test ./a11ytests/a11y.specs.ts

4. 依赖检查

确认package.json中已正确安装所需依赖:

  • @playwright/test
  • axe-playwright
  • playwright

若未安装,执行以下命令安装:

npm install @playwright/test axe-playwright --save-dev
npx playwright install

内容的提问来源于stack exchange,提问作者Abhishek Konnur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 08:06:50