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