关于 nightwatchjs-skill
nightwatchjs-skill 是 NightwatchJS 浏览器自动化参考技能,涵盖本地与云端执行、测试模式、页面对象、断言及 TestMu AI 云配置。
主要功能
- 01本地与云端执行指南
- 02浏览器自动化测试模式
- 03页面对象示例
- 04内置断言示例
- 05TestMu AI 云配置
- 06安装与运行测试命令
适用场景
- 01选择本地或云端执行目标
- 02使用 NightwatchJS 编写浏览器测试
- 03通过页面对象组织选择器
- 04在 LambdaTest 环境运行测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill nightwatchjs-skill安装后在 Claude Code 中输入/nightwatchjs-skill确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
reference/advanced-patterns.mdcloud-integration.mdplaybook.md详细介绍
NightwatchJS 自动化技能
步骤 1 — 执行目标
├─ "cloud", "TestMu", "LambdaTest" → Cloud: nightwatch.conf.js with LT env
├─ "local" → Local: ChromeDriver/GeckoDriver
└─ Default → Local, mention cloud option
核心模式
基本测试
module.exports = {
'Login with valid credentials': function(browser) {
browser
.url('http://localhost:3000/login')
.waitForElementVisible('#email', 5000)
.setValue('#email', '[email protected]')
.setValue('#password', 'password123')
.click('button[type="submit"]')
.waitForElementVisible('.dashboard', 10000)
.assert.containsText('.welcome', 'Welcome')
.assert.urlContains('/dashboard')
.end();
},
'Login with invalid credentials shows error': function(browser) {
browser
.url('http://localhost:3000/login')
.waitForElementVisible('#email', 5000)
.setValue('#email', '[email protected]')
.setValue('#password', 'wrong')
.click('button[type="submit"]')
.waitForElementVisible('.error-message', 5000)
.assert.containsText('.error-message', 'Invalid credentials')
.end();
}
};
页面对象
// pages/loginPage.js
module.exports = {
url: '/login',
elements: {
emailInput: '#email',
passwordInput: '#password',
loginButton: 'button[type="submit"]',
errorMessage: '.error-message',
},
commands: [{
login(email, password) {
return this
.setValue('@emailInput', email)
.setValue('@passwordInput', password)
.click('@loginButton');
}
}]
};
// tests/loginTest.js
module.exports = {
'Login test': function(browser) {
const login = browser.page.loginPage();
login.navigate()
.login('[email protected]', 'password123');
browser.assert.urlContains('/dashboard');
}
};
断言
browser.assert.visible(selector);
browser.assert.not.visible(selector);
browser.assert.containsText(selector, 'text');
browser.assert.urlContains('/path');
browser.assert.titleContains('Page Title');
browser.assert.elementPresent(selector);
browser.assert.cssClassPresent(selector, 'active');
browser.assert.value('#input', 'expected');
browser.assert.attributeEquals(selector, 'href', '/link');
browser.assert.elementsCount('li', 5);
TestMu AI Cloud 配置
如需了解完整功能和共享参考,请参阅 reference/cloud-integration.md。
// nightwatch.conf.js
module.exports = {
test_settings: {
default: {
launch_url: 'http://localhost:3000',
desiredCapabilities: { browserName: 'chrome' }
},
lambdatest: {
selenium: {
host: 'hub.lambdatest.com',
port: 80
},
desiredCapabilities: {
browserName: 'chrome',
browserVersion: 'latest',
'LT:Options': {
platform: 'Windows 11',
build: 'Nightwatch Build',
name: 'Login Tests',
user: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY,
video: true, console: true, network: true
}
}
}
},
page_objects_path: ['pages/'],
};
安装:npm install nightwatch --save-dev
运行:npx nightwatch 或 npx nightwatch --env lambdatest
深入模式
如需了解高级模式、调试指南、CI/CD 集成和最佳实践,
请参阅 reference/playbook.md。
评论
支持 Markdown