Capybara Automation Skill
Core Patterns
Basic Test (RSpec)
require 'capybara/rspec'
RSpec.describe 'Login', type: :feature do
it 'logs in with valid credentials' do
visit '/login'
fill_in 'Email', with: 'user@test.com'
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: 'wrong@test.com'
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 Scope
within('#login-form') do
fill_in 'Email', with: 'user@test.com'
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
Setup: gem 'capybara' + gem 'selenium-webdriver' in Gemfile
Run: bundle exec rspec spec/features/
Cloud (TestMu AI)
For remote browser execution, see reference/cloud-integration.md and shared/testmu-cloud-reference.md.
Deep Patterns
See reference/playbook.md for production-grade patterns:
| Section |
What You Get |
| §1 Project Setup |
Gemfile, Capybara config, driver registration, LambdaTest |
| §2 Feature Specs |
Login flows, JavaScript interactions, modals, async content |
| §3 Page Objects |
SitePrism pages with elements/sections, usage in specs |
| §4 API Testing |
Request specs with auth headers, JSON assertions |
| §5 Database Cleaning |
DatabaseCleaner transaction/truncation strategies |
| §6 Matchers & Helpers |
Custom helpers, sign_in, expect_flash |
| §7 CI/CD Integration |
GitHub Actions with Postgres, Redis, Chrome |
| §8 Debugging Table |
12 common problems with causes and fixes |
| §9 Best Practices |
14-item Capybara testing checklist |
1---2name: capybara-skill3description: Generates Capybara E2E tests in Ruby with RSpec integration. Acceptance testing DSL for web apps. Use when user mentions "Capybara", "visit", "fill_in", "click_button", "Ruby E2E". Triggers on: "Capybara", "Ruby acceptance test", "fill_in", "click_button", "have_content".4license: MIT5---6
7# Capybara Automation Skill
8
9## Core Patterns
10
11### Basic Test (RSpec)
12
13```ruby
14require 'capybara/rspec'
15
16RSpec.describe 'Login', type: :feature do
17 it 'logs in with valid credentials' do
18 visit '/login'
19 fill_in 'Email', with: 'user@test.com'
20 fill_in 'Password', with: 'password123'
21 click_button 'Login'
22 expect(page).to have_content('Dashboard')
23 expect(page).to have_current_path('/dashboard')
24 end
25
26 it 'shows error for invalid credentials' do
27 visit '/login'
28 fill_in 'Email', with: 'wrong@test.com'
29 fill_in 'Password', with: 'wrong'
30 click_button 'Login'
31 expect(page).to have_content('Invalid credentials')
32 end
33end
34```
35
36### DSL
37
38```ruby
39# Navigation
40visit '/path'
41go_back
42go_forward
43
44# Interacting
45fill_in 'Label or Name', with: 'text'
46choose 'Radio Label'
47check 'Checkbox Label'
48uncheck 'Checkbox Label'
49select 'Option', from: 'Select Label'
50attach_file 'Upload', '/path/to/file'
51click_button 'Submit'
52click_link 'More Info'
53click_on 'Button or Link'
54
55# Finding
56find('#id')
57find('.class')
58find('[data-testid="x"]')
59find(:xpath, '//div')
60all('.items').count
61
62# Matchers
63expect(page).to have_content('text')
64expect(page).to have_selector('#element')
65expect(page).to have_css('.class')
66expect(page).to have_button('Submit')
67expect(page).to have_field('Email')
68expect(page).to have_link('Click Here')
69expect(page).to have_current_path('/expected')
70expect(page).to have_no_content('error')
71```
72
73### Within Scope
74
75```ruby
76within('#login-form') do
77 fill_in 'Email', with: 'user@test.com'
78 click_button 'Login'
79end
80
81within_table('users') do
82 expect(page).to have_content('Alice')
83end
84```
85
86### TestMu AI Cloud
87
88```ruby
89Capybara.register_driver :lambdatest do |app|
90 caps = Selenium::WebDriver::Remote::Capabilities.new(
91 browserName: 'chrome',
92 'LT:Options' => {
93 user: ENV['LT_USERNAME'], accessKey: ENV['LT_ACCESS_KEY'],
94 build: 'Capybara Build', name: 'Login Test',
95 platform: 'Windows 11', video: true
96 }
97 )
98 Capybara::Selenium::Driver.new(app,
99 browser: :remote,
100 url: 'https://hub.lambdatest.com/wd/hub',
101 capabilities: caps)
102end
103Capybara.default_driver = :lambdatest
104```
105
106## Setup: `gem 'capybara'` + `gem 'selenium-webdriver'` in Gemfile
107## Run: `bundle exec rspec spec/features/`
108
109## Cloud (TestMu AI)
110
111For remote browser execution, see [reference/cloud-integration.md](reference/cloud-integration.md) and [shared/testmu-cloud-reference.md](../shared/testmu-cloud-reference.md).
112
113## Deep Patterns
114
115See `reference/playbook.md` for production-grade patterns:
116
117| Section | What You Get |
118|---------|-------------|
119| §1 Project Setup | Gemfile, Capybara config, driver registration, LambdaTest |
120| §2 Feature Specs | Login flows, JavaScript interactions, modals, async content |
121| §3 Page Objects | SitePrism pages with elements/sections, usage in specs |
122| §4 API Testing | Request specs with auth headers, JSON assertions |
123| §5 Database Cleaning | DatabaseCleaner transaction/truncation strategies |
124| §6 Matchers & Helpers | Custom helpers, sign_in, expect_flash |
125| §7 CI/CD Integration | GitHub Actions with Postgres, Redis, Chrome |
126| §8 Debugging Table | 12 common problems with causes and fixes |
127| §9 Best Practices | 14-item Capybara testing checklist |