关于 testcafe-skill
为使用 TestCafe 自动化浏览器测试提供参考,面向 JavaScript 测试编写者,涵盖选择器、页面模型、浏览器运行、截图和 TestMu AI 云端执行。
主要功能
- 01TestCafe 测试模式
- 02选择器与页面模型
- 03浏览器及无头运行命令
- 04多浏览器测试执行
- 05截图与窗口控制
- 06TestMu AI 云端配置
适用场景
- 01使用 JavaScript 编写浏览器测试
- 02在多个浏览器中运行测试
- 03截取截图以辅助调试
- 04在 TestMu AI 云端执行测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill testcafe-skill安装后在 Claude Code 中输入/testcafe-skill确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
reference/advanced-patterns.mdcloud-integration.mdplaybook.md详细介绍
TestCafe 自动化技能
核心模式
基础测试
import { Selector } from 'testcafe';
fixture('Login').page('https://example.com/login');
test('Login with valid credentials', async t => {
await t
.typeText('#username', '[email protected]')
.typeText('#password', 'password123')
.click('button[type="submit"]')
.expect(Selector('.dashboard').exists).ok();
});
选择器
const submitBtn = Selector('button').withText('Submit');
const listItems = Selector('.item').count;
const nthItem = Selector('.item').nth(2);
const filtered = Selector('.item').withAttribute('data-status', 'active');
页面模型
import { Selector, t } from 'testcafe';
class LoginPage {
constructor() {
this.usernameInput = Selector('#username');
this.passwordInput = Selector('#password');
this.submitButton = Selector('button[type="submit"]');
}
async login(username, password) {
await t
.typeText(this.usernameInput, username)
.typeText(this.passwordInput, password)
.click(this.submitButton);
}
}
export default new LoginPage();
TestMu AI Cloud
有关完整的云端设置,请参阅 reference/cloud-integration.md;有关功能,请参阅 shared/testmu-cloud-reference.md。
export LT_USERNAME=your_username
export LT_ACCESS_KEY=your_key
npx testcafe "lambdatest:Chrome@latest:Windows 11" tests/
反模式
| 不佳做法 | 推荐做法 | 原因 |
|---|---|---|
await t.wait(5000) | 设置超时的智能断言 | 任意延迟 |
| 深层 CSS 选择器 | Selector().withText() | 脆弱 |
| 不截取错误截图 | 失败时调用 t.takeScreenshot() | 缺少调试信息 |
快速参考
| 任务 | 命令 |
|---|---|
| 运行全部测试 | npx testcafe chrome tests/ |
| 无头运行 | npx testcafe chrome:headless tests/ |
| 运行指定测试 | npx testcafe chrome tests/login.js |
| 多个浏览器 | npx testcafe chrome,firefox tests/ |
| 实时模式 | npx testcafe chrome tests/ --live |
| 截图 | await t.takeScreenshot() |
| 调整大小 | await t.resizeWindow(1280, 720) |
深入模式
有关高级模式、调试指南、CI/CD 集成和最佳实践,
请参阅 reference/playbook.md。
评论
支持 Markdown