使用Playwright创建测试时提示「未找到测试」的问题求助
解决Playwright "Error: No tests found" 问题
问题根源
你的package.json里的test脚本还是默认的提示语句,没有调用Playwright的测试命令;同时缺少符合Playwright规范的测试文件结构。
解决方案步骤
- 修正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" } }
- 创建测试文件
在项目根目录新建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'); });
- 安装Playwright浏览器
执行命令安装测试所需的浏览器:
npx playwright install
- 运行测试
执行npm test即可正常触发Playwright测试。
额外说明
- Playwright默认识别
tests目录下的*.spec.js/*.test.js格式文件,也可以通过配置文件自定义测试路径和命名规则。 - 若需要自定义测试配置,可执行
npx playwright init生成playwright.config.js文件进行调整。
内容的提问来源于stack exchange,提问作者Marija Čolaković
相关产品推荐
相关产品推荐

