关于 capybara-skill
capybara-skill 是编写 Capybara 浏览器测试的参考技能,面向使用 RSpec 的测试开发者。内容涵盖交互模式、页面对象、API 与数据库测试、调试及 CI/CD 配置。
主要功能
- 01RSpec 功能测试模式
- 02浏览器导航与表单交互
- 03限定页面和表格查询范围
- 04页面对象与 API 测试
- 05数据库清理和 CI/CD 指引
适用场景
- 01编写基于浏览器的登录和交互测试
- 02用页面对象和可复用辅助方法组织测试
- 03排查常见的 Capybara 测试故障
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill capybara-skill安装后在 Claude Code 中输入/capybara-skill确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
reference/advanced-patterns.mdcloud-integration.mdplaybook.md详细介绍
Capybara 自动化技能
核心模式
基本测试(RSpec)
require 'capybara/rspec'
RSpec.describe 'Login', type: :feature do
it 'logs in with valid credentials' do
visit '/login'
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'password123'
click_button 'Login'
expect(page).to have_content('Dashboard')
expect(page).to have_current_path('/dashboard')
end
it 'shows error for invalid credentials' do
visit '/login'
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'wrong'
click_button 'Login'
expect(page).to have_content('Invalid credentials')
end
end
DSL
# Navigation
visit '/path'
go_back
go_forward
# Interacting
fill_in 'Label or Name', with: 'text'
choose 'Radio Label'
check 'Checkbox Label'
uncheck 'Checkbox Label'
select 'Option', from: 'Select Label'
attach_file 'Upload', '/path/to/file'
click_button 'Submit'
click_link 'More Info'
click_on 'Button or Link'
# Finding
find('#id')
find('.class')
find('[data-testid="x"]')
find(:xpath, '//div')
all('.items').count
# Matchers
expect(page).to have_content('text')
expect(page).to have_selector('#element')
expect(page).to have_css('.class')
expect(page).to have_button('Submit')
expect(page).to have_field('Email')
expect(page).to have_link('Click Here')
expect(page).to have_current_path('/expected')
expect(page).to have_no_content('error')
限定范围
within('#login-form') do
fill_in 'Email', with: '[email protected]'
click_button 'Login'
end
within_table('users') do
expect(page).to have_content('Alice')
end
TestMu AI Cloud
Capybara.register_driver :lambdatest do |app|
caps = Selenium::WebDriver::Remote::Capabilities.new(
browserName: 'chrome',
'LT:Options' => {
user: ENV['LT_USERNAME'], accessKey: ENV['LT_ACCESS_KEY'],
build: 'Capybara Build', name: 'Login Test',
platform: 'Windows 11', video: true
}
)
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: 'https://hub.lambdatest.com/wd/hub',
capabilities: caps)
end
Capybara.default_driver = :lambdatest
设置:在 Gemfile 中添加 gem 'capybara' + gem 'selenium-webdriver'
运行:bundle exec rspec spec/features/
云端(TestMu AI)
如需远程浏览器执行,请参阅 reference/cloud-integration.md 和 shared/testmu-cloud-reference.md。
深入模式
生产级模式请参阅 reference/playbook.md:
| 章节 | 内容 |
|---|---|
| §1 项目设置 | Gemfile、Capybara 配置、驱动注册、LambdaTest |
| §2 功能规格 | 登录流程、JavaScript 交互、模态框、异步内容 |
| §3 页面对象 | 带有元素/区块的 SitePrism 页面,以及在规格中的用法 |
| §4 API 测试 | 带有身份验证标头的请求规格、JSON 断言 |
| §5 数据库清理 | DatabaseCleaner 事务/截断策略 |
| §6 匹配器与辅助工具 | 自定义辅助工具、sign_in、expect_flash |
| §7 CI/CD 集成 | 使用 Postgres、Redis、Chrome 的 GitHub Actions |
| §8 调试表 | 12 个常见问题及其原因和修复方法 |
| §9 最佳实践 | 包含 14 项的 Capybara 测试清单 |
评论
支持 Markdown