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

使用Playwright创建测试时提示「未找到测试」的问题求助

解决Playwright "Error: No tests found" 问题

问题根源

你的package.json里的test脚本还是默认的提示语句,没有调用Playwright的测试命令;同时缺少符合Playwright规范的测试文件结构。

解决方案步骤

  1. 修正package.json的test脚本
    将默认的test脚本替换为Playwright的测试命令:
{
  "name": "playwright-course",
  "version": "1.0.0",
  "description": "playwright course",
  "main": "index.js",
  "scripts": {
    "test": "playwright test"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@playwright/test": "^1.39.0"
  }
}
  1. 创建测试文件
    在项目根目录新建tests文件夹,在其中创建测试文件(如example.spec.js),写入基础测试代码:
const { test, expect } = require('@playwright/test');

test('基础页面测试', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const标题元素 = page.locator('.navbar__inner .navbar__title');
  await expect(标题元素).toHaveText('Playwright');
});
  1. 安装Playwright浏览器
    执行命令安装测试所需的浏览器:
npx playwright install
  1. 运行测试
    执行npm test即可正常触发Playwright测试。

额外说明

  • Playwright默认识别tests目录下的*.spec.js/*.test.js格式文件,也可以通过配置文件自定义测试路径和命名规则。
  • 若需要自定义测试配置,可执行npx playwright init生成playwright.config.js文件进行调整。

内容的提问来源于stack exchange,提问作者Marija Čolaković

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 20:37:49