工具对比
工具 | 是否开源 | 许可证 | 注意点 |
---|---|---|---|
Cypress | ✅ 核心免费开源 | MIT 许可GitHubdocs.cypress.io | 仅桌面运行器和 CLI 是 MIT;若要把 CI 结果上传到官方云(Cypress Cloud),那部分是 SaaS 服务,按团队人数/并发收费。 |
Playwright | ✅ 完全开源 | Apache‑2.0 许可GitHub | 纯开源,没有商业版区分,浏览器二进制由脚手架一次性下载。 |
前置:确保已安装 Node.js ≥ 18(node -v
)。
1、初始化 package.json
# 进入项目根目录
$ cd crm-admin
$ npm init -y
2、安装工具
$ npm install –save-dev cypress playwright
安装时间比较长,请耐心等待
3、配置 package.json
脚本命令
编辑 package.json
,在 "scripts"
字段里加:
“scripts”: {
“test:cypress”: “cypress open”,
“test:playwright”: “playwright test”
}
这样你可以:
-
npm run test:cypress
打开 Cypress 可视化测试界面; -
npm run test:playwright
运行 Playwright 自动化测试。
4、测试示例文件
Cypress 示例
-
在项目根目录新建
cypress/e2e/sample.cy.js
,内容:
describe(‘首页测试’, () => {
it(‘访问主页,检查标题’, () => {
cy.visit(‘http://localhost:8000’) // 本地运行的 ThinkPHP 网站地址
cy.title().should(‘include’, ‘房产CRM’) // 检查网页标题包含‘房产CRM’
})
})
Playwright 示例
-
在项目根目录新建
tests/e2e/sample.spec.js
,内容:
const { test, expect } = require(‘@playwright/test’);
test(‘首页加载测试’, async ({ page }) => {
await page.goto(‘http://localhost:8000’);
await expect(page).toHaveTitle(/房产CRM/);
});
5、运行测试
1) 启动你的 ThinkPHP 本地服务器(比如用内置PHP服务器):
php think run # 具体命令根据你项目调整
2)运行测试:
npm run test:cypress # 打开 Cypress 界面,可以执行测试
npm run test:playwright # 执行 Playwright 测试脚本