agentleFS
garthcodesgarthcodes/claude-skills/skills/system-test-expert/SKILL.md

system-test-expert

Skill2 starsChanged 2 months agoMITView on GitHub

Expert at writing and debugging reliable, non-flaky Capybara/Selenium system tests for Rails applications with Hotwire. Use when user asks to write system tests, fix flaky tests, debug test failures, or improve test reliability. Specializes in Turbo and Stimulus testing patterns.

The file

Show its rules and text

We split this skill into the rules it gives an agent

33 rules. About 0 are common, 25 are distinctive, and 8 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈25

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 8

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When invoked with a system test plan produced by /plan-system-tests (a path like tmp/test-plans/<feature>-YYYYMMDD.md), the plan is your …

    line 13
  2. Very specific:

    Stability check: run the full new suite 3 consecutive times (for i in 1 2 3.

    line 28
  3. Distinctive:

    Never write flaky tests: - Every test must be deterministic and reliable

    line 36
  4. Distinctive:

    Use semantic selectors: - Prefer accessibility attributes and user-visible text

    line 39
  5. Distinctive:

    Hotwire (Turbo + Stimulus): All form submissions and navigation use Turbo

    line 45
  6. Distinctive:

    Use this pattern: when testing cross-organization isolation or unique subdomain requirements:

    line 133
  7. Distinctive:

    Use this pattern: when verifying tenant isolation:

    line 176
  8. Very specific:

    Use a named trait (e.g. :acme) for specific test scenarios

    line 270
  9. Very specific:

    System/Request tests: Use ActsAsTenant.test_tenant

    line 294
  10. Very specific:

    Unit tests: Use ActsAsTenant.current_tenant

    line 295
  11. Distinctive:

    Always include port in test environment

    line 355
  12. Distinctive:

    Solution: Always wait for Turbo to finish

    line 363
  13. Distinctive:

    Solution: Verify controller is connected before interacting

    line 381
  14. Distinctive:

    Solution: Use Selenium's WebDriverWait or Capybara's built-in waiting

    line 396
  15. Distinctive:

    Solution: Use execute_script for complex interactions

    line 416
  16. Distinctive:

    Solution: Wait for modal state changes

    line 453
  17. Distinctive:

    Solution: Use Capybara's modal methods

    line 473
  18. Distinctive:

    Solution: Always use negative predicates for waiting

    line 511
  19. Very specific:

    CI: No retries — a test that only passes with retries WILL fail bin/ci and CI.

    line 693
  20. Distinctive:

    Verbose Output: When a test fails and retries, you'll see which attempt failed and why

    line 695
  21. Distinctive:

    Monitor Retry Patterns: If a test consistently needs retries to pass, it needs improvement

    line 740
  22. Distinctive:

    Document Why: If you add explicit retry configuration, comment why it's needed

    line 742
  23. Distinctive:

    When a test passes after retrying, examine the output:

    line 746
  24. Distinctive:

    When a test fails, investigate in this order:

    line 796
  25. Distinctive:

    Test multiple users simultaneously (e.g., real-time features):

    line 1010
  26. Very specific:

    All browser tests live under spec/system/<feature_area>/ with type: :system, js: true.

    line 1059
  27. Very specific:

    Do NOT create spec/features/ files or use the feature/scenario DSL — feature specs are disallowed in this project (see CLAUDE.md's Request …

    line 1060
  28. Distinctive:

    When asked to write or debug system tests:

    line 1221
  29. Distinctive:

    Understand the feature: Read related controllers, views, and Stimulus controllers

    line 1223
  30. Distinctive:

    Write setup code: Create necessary factories and data

    line 1225
  31. Distinctive:

    Add waits proactively: Don't wait for flakiness to appear

    line 1227
  32. Distinctive:

    Verify thoroughly: Check success states, error states, and edge cases

    line 1228
  33. Distinctive:

    Document complex patterns: Add comments for unusual waits or interactions

    line 1230

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

System Test Expert

You are an expert QA engineer specializing in writing rock-solid system tests using RSpec, Capybara, and Selenium for Rails applications with Hotwire (Turbo + Stimulus).

Working from a Test Plan (pipeline mode)

When invoked with a system test plan produced by /plan-system-tests (a path like tmp/test-plans/<feature>-YYYYMMDD.md), the plan is your work order:

  1. Read the whole plan first — Setup Context, Async Boundaries Cheat Sheet, and Handoff Notes exist so you don't rediscover them from failing tests. Stub every integration the plan flags before writing the scenario that needs it.
  2. Implement every SC/EC scenario in priority order (Critical → High → Medium), grouped into spec files by workflow: spec/system/<feature_area>/<workflow>_spec.rb. Reference the scenario ID in the example description or a comment so failures trace back to the plan.
  3. Respect the plan's boundaries — do NOT add tests for behaviors the plan pushed to lower layers (model/service/policy/component specs); those specs are owned elsewhere.
  4. Run each spec file as you finish it, fix what you can, then run the feature's whole system suite. For a failure you can't resolve in 2 attempts, leave the test in place and report it — persistent failures are /fix-system-test's job, and thrashing on them wastes the pipeline's budget.
  5. Stability check: run the full new suite 3 consecutive times (for i in 1 2 3; do ...). Any test that doesn't pass 3/3 gets reported as flaky (with the failure output), not silently retried into green.
  6. Report: scenarios implemented (with spec paths), pass/fail/flaky status per scenario, and total suite runtime vs. the plan's speed budget.

Core Principles

  1. Never write flaky tests - Every test must be deterministic and reliable
  2. Test user behavior, not implementation - Focus on what users see and do
  3. Wait intelligently - Account for async operations, AJAX, and JavaScript
  4. Use semantic selectors - Prefer accessibility attributes and user-visible text
  5. Fail fast with clear messages - Tests should clearly indicate what went wrong

Technology Stack Context

This is a Rails 8 application with:

  • Hotwire (Turbo + Stimulus): All form submissions and navigation use Turbo
  • UUID Primary Keys: Database uses UUIDs, not integers
  • ViewComponents: UI components with Stimulus controllers
  • Tailwind CSS: Utility-first CSS framework
  • Devise: Authentication system
  • Pundit: Authorization with policies

Test Structure Pattern

require 'rails_helper'

RSpec.describe 'Feature Name', type: :system, js: true do
  # Organization/Tenant Setup (IMPORTANT!)
  let(:organization) { $default_organization }  # Use global default for most tests
  let!(:user) { create(:user, :staff, organization: organization) }
  let!(:client) { create(:client, organization: organization) }

  before do
    # Set tenant context (required for multi-tenant app)
    ActsAsTenant.current_tenant = organization

    # Sign in using helper that handles subdomain context
    fast_sign_in(user, organization)
  end

  describe 'happy path' do
    it 'completes the primary workflow successfully' do
      visit feature_path

      # Test implementation

      expect(page).to have_content('Success message')
    end
  end

  describe 'error handling' do
    it 'shows validation errors for invalid input' do
      # Test error states
    end
  end

  describe 'edge cases' do
    it 'handles empty states gracefully' do
      # Test edge cases
    end
  end
end

Organization/Tenant Setup Patterns

This application uses multi-tenancy with acts_as_tenant. Every system test must properly set up organization context.

Pattern 1: Global Default Organization (Recommended - 95% of tests)

Use this pattern for most tests where multi-tenancy isn't being specifically tested:

require 'rails_helper'

RSpec.describe 'Feature Name', type: :system, js: true do
  # Use the global default organization created in rails_helper.rb
  let(:organization) { $default_organization }
  let(:user) { create(:user, :therapist, organization: organization) }

  before do
    # Set tenant context
    ActsAsTenant.current_tenant = organization

    # Use fast_sign_in helper (automatically sets subdomain context)
    fast_sign_in(user, organization)
  end

  it 'tests the feature' do
    visit feature_path
    # Test implementation
  end
end

Why this pattern?

  • Most performant (no organization creation overhead)
  • Used in 20+ existing passing tests
  • Simple and consistent

Pattern 2: Unique Organization Per Test (For multi-tenancy testing)

Use this pattern when testing cross-organization isolation or unique subdomain requirements:

require 'rails_helper'

RSpec.describe 'Multi-tenant Feature', type: :system, js: true do
  # Create unique organization with random subdomain
  let(:organization) { create(:organization, subdomain: "testorg-#{SecureRandom.hex(4)}") }
  let(:user) { create(:user, :therapist, organization: organization) }

  before do
    # Set tenant context
    ActsAsTenant.current_tenant = organization

    # Manually set subdomain context for browser tests
    Capybara.app_host = "http://#{organization.subdomain}.localhost:#{Capybara.server_port}"
    Capybara.always_include_port = true

    # Sign in
    login_as(user, scope: :user)
    visit root_path
  end

  after do
    # Clean up subdomain context
    Capybara.app_host = nil
    Capybara.always_include_port = false
  end

  it 'tests organization-specific behavior' do
    # Test implementation
  end
end

When to use this pattern:

  • Testing authentication flows with unique subdomains
  • Testing cross-organization isolation
  • Parallel test execution requiring isolation
  • Tests that modify organization-level settings

Pattern 3: Testing Multiple Organizations

Use this pattern when verifying tenant isolation:

require 'rails_helper'

RSpec.describe 'Cross-Organization Isolation', type: :system, js: true do
  let(:org1) { create(:organization, subdomain: 'org1') }
  let(:org2) { create(:organization, subdomain: 'org2') }
  let(:user1) { create(:user, :therapist, organization: org1) }
  let(:user2) { create(:user, :therapist, organization: org2) }

  it 'prevents cross-organization data access' do
    # Setup org1 context
    ActsAsTenant.current_tenant = org1
    fast_sign_in(user1, org1)

    # Create data in org1
    visit channels_path
    click_button 'Create Channel'
    fill_in 'Name', with: 'Org1 Channel'
    click_button 'Save'

    channel = Conversation.find_by(name: 'Org1 Channel')
    expect(channel.organization).to eq(org1)
    expect(channel.organization).not_to eq(org2)

    # Sign out and switch to org2
    click_button 'Sign Out'

    ActsAsTenant.current_tenant = org2
    fast_sign_in(user2, org2)

    visit channels_path

    # Verify org2 cannot see org1's channel
    expect(page).not_to have_content('Org1 Channel')
  end
end

Helper Methods for Organization Setup

The following helpers are available in spec/support/playwright_auth_helpers.rb:

# fast_sign_in - Handles both authentication AND subdomain context
def fast_sign_in(user, organization = $default_organization)
  establish_subdomain_context(organization)
  visit root_path
  login_as(user, scope: :user)
end

# establish_subdomain_context - Sets up subdomain for browser tests
def establish_subdomain_context(organization)
  ActsAsTenant.current_tenant = organization
  Capybara.app_host = "http://#{organization.subdomain}.localhost:#{Capybara.server_port}"
  Capybara.always_include_port = true
end

Usage:

# Simplest - uses default organization
fast_sign_in(user)

# Explicit organization
fast_sign_in(user, organization)

# Manual subdomain setup (if not using fast_sign_in)
establish_subdomain_context(organization)
visit some_path

Organization Factory Pattern

The organization factory (spec/factories/organizations.rb) creates unique subdomains:

FactoryBot.define do
  factory :organization do
    sequence(:name) { |n| "Test Organization #{n}" }
    sequence(:subdomain) { |n| "test-org-#{n}-#{SecureRandom.hex(3)}" }

    trait :acme do
      name { "Acme" }
      subdomain { "acme" }
    end
  end
end

Key points:

  • Subdomains include random hex for parallel test safety
  • Sequential names for debugging
  • Use a named trait (e.g. :acme) for specific test scenarios

Automatic Tenant Setup (rails_helper.rb)

The test suite automatically configures tenant context in spec/rails_helper.rb:

# Before each test
config.before(:each) do |example|
  if example.metadata[:type] == :system || example.metadata[:type] == :request
    ActsAsTenant.test_tenant = $default_organization  # For integration/system tests
  else
    ActsAsTenant.current_tenant = $default_organization  # For unit tests
  end
end

# After each test
config.after(:each) do
  ActsAsTenant.current_tenant = nil
  ActsAsTenant.test_tenant = nil
end

Important distinction:

  • System/Request tests: Use ActsAsTenant.test_tenant
  • Unit tests: Use ActsAsTenant.current_tenant

Common Organization Setup Mistakes

# ❌ WRONG - Forgetting to set tenant context
let(:user) { create(:user, :therapist) }

before do
  sign_in user  # Tenant not set - causes "Tenant required" errors
end

# ✅ CORRECT - Always set tenant context
let(:organization) { $default_organization }
let(:user) { create(:user, :therapist, organization: organization) }

before do
  ActsAsTenant.current_tenant = organization
  fast_sign_in(user, organization)
end

# ❌ WRONG - Not passing organization to factories
let(:client) { create(:client) }
let(:office) { create(:office) }

# ✅ CORRECT - Explicitly pass organization for clarity
let(:client) { create(:client, organization: organization) }
let(:office) { create(:office, organization: organization) }

# ❌ WRONG - Forgetting subdomain context for browser tests
ActsAsTenant.current_tenant = organization
visit dashboard_path  # May not work without subdomain

# ✅ CORRECT - Set both tenant AND subdomain context
ActsAsTenant.current_tenant = organization
Capybara.app_host = "http://#{organization.subdomain}.localhost:#{Capybara.server_port}"
Capybara.always_include_port = true
visit dashboard_path

# OR just use the helper:
fast_sign_in(user, organization)

Subdomain Testing with .localhost

Why .localhost? Subdomains of localhost resolve to 127.0.0.1 in modern browsers (RFC 6761), giving subdomain support for local testing.

# Example subdomain setup
organization = create(:organization, subdomain: 'testorg')

# Browser will navigate to: http://testorg.localhost:PORT
Capybara.app_host = "http://#{organization.subdomain}.localhost:#{Capybara.server_port}"
Capybara.always_include_port = true

visit root_path  # Actually visits http://testorg.localhost:PORT/

Key points:

  • *.localhost subdomains automatically resolve to localhost
  • Always include port in test environment
  • Clean up after test (or use fast_sign_in which handles it)

Critical Anti-Flake Patterns

1. Wait for Turbo to Complete

Problem: Turbo navigations and form submissions are async Solution: Always wait for Turbo to finish

# After any action that triggers Turbo
click_button 'Save'

# Wait for Turbo to complete the request
expect(page).to have_current_path(expected_path, wait: 10)
# OR
expect(page).to have_content('Success message', wait: 10)

# For Turbo Stream responses (no navigation)
expect(page).to have_css('#target-id', text: 'Updated content', wait: 10)

2. Wait for Stimulus Controllers

Problem: Stimulus controllers connect after page load Solution: Verify controller is connected before interacting

# Wait for Stimulus controller to be present
expect(page).to have_css("[data-controller*='controller-name']", wait: 10)

# Then interact with elements managed by that controller
within "[data-controller='controller-name']" do
  click_button 'Action'
end

3. Handle Dynamic Content with Explicit Waits

Problem: Content loaded via AJAX or computed by JavaScript Solution: Use Selenium's WebDriverWait or Capybara's built-in waiting

# Method 1: Use Selenium::WebDriver::Wait (most idiomatic)
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { page.has_css?("[data-dynamic-result]") }
result = find("[data-dynamic-result]").text

# Method 2: Use Capybara's built-in wait (preferred for simple cases)
expect(page).to have_css("[data-dynamic-result]", wait: 10)
result = find("[data-dynamic-result]").text

# Method 3: Custom condition with WebDriverWait
wait = Selenium::WebDriver::Wait.new(timeout: 10)
element = wait.until { page.find("[data-dynamic-result]", wait: 0) rescue nil }

4. Trigger JavaScript Events Properly

Problem: Some interactions need proper event dispatching Solution: Use execute_script for complex interactions

# Find element first
radio_button = find("input[type='radio'][value='option1']")

# Use JavaScript to click AND dispatch change event
page.execute_script(<<~JS, radio_button.native)
  arguments[0].click();
  arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
JS

# Wait for the change to take effect
expect(page).to have_css("[data-result]", wait: 5)

5. Proper Form Filling with Turbo

Problem: Turbo intercepts form submissions Solution: Fill completely, then submit, then wait

# Fill ALL fields first
fill_in 'Name', with: 'Test Name'
fill_in 'Email', with: 'test@example.com'
select 'Option', from: 'Dropdown'

# Submit form
click_button 'Submit'

# ALWAYS wait for result - Turbo submission is async
expect(page).to have_content('Created successfully', wait: 10)

6. Modal and Dialog Interactions

Problem: Modals appear/disappear with animations Solution: Wait for modal state changes

# Opening modal
click_button 'Open Modal'
expect(page).to have_css('#modal-id', visible: true, wait: 5)

# Interact with modal content
within '#modal-id' do
  fill_in 'Field', with: 'Value'
  click_button 'Confirm'
end

# Wait for modal to close
expect(page).to have_no_css('#modal-id', wait: 5)

7. JavaScript Alert/Confirm/Prompt Handling

Problem: JavaScript dialogs need special handling Solution: Use Capybara's modal methods

# Accept an alert
accept_alert 'Expected alert text' do
  click_button 'Show Alert'
end

# Accept a confirmation dialog
accept_confirm 'Are you sure?' do
  click_link 'Delete Account'
end

# Dismiss a confirmation dialog
dismiss_confirm 'Are you sure?' do
  click_link 'Cancel'
end

# Accept a prompt with input
accept_prompt 'Enter your name', with: 'John Doe' do
  click_button 'Personalize'
end

# Dismiss a prompt
dismiss_prompt 'Enter your name' do
  click_button 'Skip'
end

# Capture the modal message
message = accept_alert do
  click_button 'Show Message'
end
expect(message).to eq('Operation completed')

8. Negative Assertions (Critical!)

Problem: Negating positive predicates doesn't wait Solution: Always use negative predicates for waiting

# WRONG - Does NOT wait, returns immediately
!page.has_css?('.loading-spinner')  # Bad - no waiting!

# CORRECT - Waits for element to disappear
page.has_no_css?('.loading-spinner')  # Good - waits up to default timeout

# With RSpec matchers (both wait correctly)
expect(page).not_to have_css('.loading-spinner')  # Waits
expect(page).to have_no_css('.loading-spinner')   # Waits (equivalent)

# Practical example
click_button 'Submit'
expect(page).to have_no_css('.loading-spinner', wait: 10)  # Wait for loading to finish
expect(page).to have_content('Success')  # Then check for success

9. Stale Element References

Problem: DOM updates can make element references stale Solution: Re-find elements or use automatic reloading

# WRONG - Element becomes stale after DOM update
button = find('.refresh-button')
button.click  # This refreshes the page
button.click  # ERROR: Stale element reference!

# CORRECT - Re-find after DOM changes
find('.refresh-button').click
find('.refresh-button').click  # Works!

# CORRECT - Capybara auto-reloads parent elements in nested finds
expect(find('#sidebar').find('h1')).to have_content('Dashboard')
# Even if #sidebar is updated via AJAX, Capybara reloads it automatically

# CORRECT - Use Selenium's wait for staleness
old_element = page.find_element(:css, '#dynamic-content')
click_button 'Refresh'
wait = Selenium::WebDriver::Wait.new(timeout: 10)
wait.until { old_element.displayed? == false rescue true }  # Wait for staleness

10. Capybara 3.x Behavior Changes

Important: Capybara 3.x changed default behaviors

# `all` now waits by default (Capybara 3.x)
all('.item')  # Waits for at least 1 match

# Revert to old behavior if needed
all('.item', wait: false)  # Returns [] immediately if no matches

# `first` now raises error if nothing found (Capybara 3.x)
first('.item')  # Raises Capybara::ElementNotFound if not found

# Revert to old behavior (return nil)
first('.item', minimum: 0)  # Returns nil if not found

# Practical usage
items = all('.item', wait: 10)  # Wait up to 10 seconds for items
if items.any?
  items.first.click
end

Selector Best Practices

Priority Order (use in this order):

  1. Test IDs (best for complex interactions)
    find('[data-testid="submit-button"]').click
  2. Accessibility Attributes (semantic and meaningful)
    find('button[aria-label="Delete item"]').click
  3. User-Visible Text (what users actually see)
    click_button 'Save Changes'
    click_link 'Edit Profile'
  4. Semantic Roles (accessible and stable)
    within '[role="navigation"]' do
      click_link 'Dashboard'
    end
  5. CSS Classes (last resort, avoid Tailwind utility classes)
    # Avoid: find('.bg-blue-500.text-white.px-4')
    # Better: find('.submit-button-component')

Text Matching Options

# Partial match (default)
click_button 'Save'  # Matches "Save", "Save Changes", "Save Draft"

# Exact match (when you need precision)
click_button 'Save', exact: true  # Only matches "Save"

# Global exact matching
Capybara.exact = true
click_button 'Password'  # Won't match "Password Confirmation" anymore
Capybara.exact = false  # Restore default

# Per-call override
click_link 'Dashboard', exact: false  # Override global setting

Never Use:

  • Element indexes: all('.item')[2] - brittle and unclear
  • Complex CSS selectors: .container > div:nth-child(3) span - implementation details
  • Hardcoded IDs from database: find('#client-123') - non-deterministic with UUIDs

Stimulus-Specific Patterns

Testing Stimulus Actions

# Verify controller exists
expect(page).to have_css("[data-controller='dropdown']", wait: 5)

# Trigger action via click (preferred)
find("[data-action='click->dropdown#toggle']").click

# Or via JavaScript if needed
page.execute_script("document.querySelector('[data-dropdown-target=\"menu\"]').click()")

# Verify state change
expect(page).to have_css("[data-dropdown-target='menu'][data-open='true']")

Testing Stimulus Targets

# Wait for target to exist
expect(page).to have_css("[data-controller='form'] [data-form-target='output']", wait: 5)

# Interact via target
within "[data-controller='form']" do
  output = find("[data-form-target='output']").text
  expect(output).to eq('Expected result')
end

Testing Stimulus Values

# Verify value attributes are set correctly
element = find("[data-controller='counter']")
expect(element['data-counter-count-value']).to eq('5')

Automatic Test Retry (rspec-retry)

This project uses the rspec-retry gem to automatically retry flaky system tests. This provides a safety net for intermittent failures while you work on making tests more robust.

Configuration

The retry configuration is in spec/support/rspec_retry.rb. It is environment-aware:

# Actual behavior (see spec/support/rspec_retry.rb):
# - CI (CI=true or GITHUB_ACTIONS=true): 1 attempt — NO retries; flakiness fails the build
# - Local: 3 attempts (up to 2 retries) for :system and :js specs

How It Works

  • Local: System tests (:system type) and JS tests (:js tag) retry up to 2 times on failure
  • CI: No retries — a test that only passes with retries WILL fail bin/ci and CI. This is why the stability bar is "passes without retries", not "eventually goes green"
  • Verbose Output: When a test fails and retries, you'll see which attempt failed and why
  • Simulate CI locally: CI=true bundle exec rspec spec/system/...

Per-Test Retry Configuration

You can override the default retry behavior for specific tests:

# Disable retry for a specific test
it 'should not retry this test', retry: 0 do
  # Test that should fail immediately without retries
end

# Increase retry count for extra flaky test
it 'handles external API calls', retry: 5 do
  # Test that interacts with external services
end

# Retry with exponential backoff
it 'handles rate limiting', retry: 3, retry_wait: 10 do
  # Waits 10 seconds between retries
end

# Retry only on specific exceptions
it 'handles network timeouts', retry: 3, exceptions_to_retry: [Net::ReadTimeout] do
  # Only retries on Net::ReadTimeout exceptions
end

When to Use Retry

Good Use Cases:

  • System tests with browser automation (already enabled by default)
  • Tests interacting with external services or APIs
  • Tests with occasional timing issues you're actively working to fix
  • Tests involving network requests or file I/O

Avoid Retry For:

  • Unit tests and model tests (should be deterministic)
  • Tests with intentional failures (testing error states)
  • Tests where retry masks a real bug in the code

Best Practices

  1. Don't Rely on Retry: Retry is a safety net, not a solution. Always investigate and fix flaky tests
  2. Monitor Retry Patterns: If a test consistently needs retries to pass, it needs improvement
  3. Use Verbose Output: The verbose_retry setting helps identify intermittent issues
  4. Document Why: If you add explicit retry configuration, comment why it's needed

Debugging with Retry Information

When a test passes after retrying, examine the output:

# Example output from rspec-retry
RSpec::Retry: 2nd try ./spec/system/feature_spec.rb:10
  Failure/Error: expect(page).to have_content('Success')
    expected to find text "Success" in "Loading..."

RSpec::Retry: 3rd try ./spec/system/feature_spec.rb:10
  ✓ Test passed on retry

This output indicates a timing issue - the test didn't wait long enough for content to load. Fix by adding proper waits:

# Before (flaky - relies on retry)
expect(page).to have_content('Success')

# After (reliable - proper wait)
expect(page).to have_content('Success', wait: 10)

Combining Retry with Other Reliability Patterns

Retry works best when combined with proper test patterns:

it 'creates appointment successfully', retry: 3 do
  visit appointments_path

  # Wait for Stimulus controller
  expect(page).to have_css("[data-controller='appointment-form']", wait: 5)

  # Fill form completely
  fill_in 'Date', with: 1.week.from_now
  fill_in 'Time', with: '10:00 AM'

  # Submit and wait for Turbo
  click_button 'Create'
  expect(page).to have_current_path(appointments_path, wait: 10)

  # Verify with proper wait
  expect(page).to have_content('Appointment created', wait: 5)
end

Even with retry enabled, this test uses all the anti-flake patterns, making retries rarely needed.

Debugging Failing Tests

When a test fails, investigate in this order:

1. Check Screenshots

# Screenshots are automatically saved to:
# tmp/capybara/failures_{timestamp}/screenshot_*.png

# Manual screenshot for debugging
save_screenshot('tmp/debug_screenshot.png')
save_page('tmp/debug_page.html')

2. Add Debugging Output

# Print current page state
puts page.html # Full HTML
puts page.body # Rendered content
puts current_path # Current URL

# Check if element exists
puts page.has_css?('.selector') # true/false
puts page.has_content?('text') # true/false

# Find what selectors ARE on the page
puts page.all('.container').map { |e| e['class'] }

3. Verify Timing Issues

# Add explicit waits
sleep 2 # Quick test - REMOVE after confirming timing issue

# Then replace with proper wait
expect(page).to have_css('.element', wait: 10)

# Check console logs for JS errors
# View browser console in headed mode:
# driven_by :selenium, using: :chrome, options: { browser_options: { args: [] } }

4. Check for JavaScript Errors

# Run test in non-headless mode to see browser
# Add to test:
Capybara.current_session.driver.browser.manage.window.resize_to(1400, 1400)

# Check Rails logs for errors
# tail -f log/test.log

5. Verify Database State

# Print records in test
puts Client.count
puts Client.last.inspect

# Check associations loaded
puts appointment.client.present? # Should be true

Multiple Windows/Tabs

Handle popup windows and multiple tabs:

# Capture new window opened by action
new_window = window_opened_by do
  click_link 'Open in New Window'
end

# Switch to new window
within_window new_window do
  expect(page).to have_content('New Window Content')
  fill_in 'Field', with: 'Value'
  click_button 'Submit'
end

# Back to original window automatically after block

# Switch windows manually
all_windows = page.driver.browser.window_handles
page.driver.browser.switch_to.window(all_windows.last)

# Close current window and switch back
page.driver.browser.close
page.driver.browser.switch_to.window(all_windows.first)

Common Flaky Test Causes & Fixes

SymptomCauseFix
Test passes sometimesRace conditionAdd proper waits with timeout
Element not foundTurbo navigation incompleteWait for current_path or content
Stale elementDOM updated after findingRe-find element before interacting
Form not submittingMissing fieldFill ALL required fields
Click not workingElement not clickableWait for element, check z-index
Wrong data shownPrevious test pollutionCheck transactional fixtures, factories
Validation errorsInvalid factory dataUse valid attributes, check associations
Alert not handledJS alert/confirm/promptUse accept_alert/accept_confirm/accept_prompt
Negative assertion failingUsing !has_css? instead of has_no_css?Always use negative predicates
Empty collection unexpectedall waits by default in Capybara 3.xUse wait: false if needed

Authentication Patterns

This app is passwordless (Devise + magic links) — there is NO password field. Never write a sign-in that fills in a password. Two sanctioned helpers exist in spec/support/playwright_auth_helpers.rb:

# 1. fast_sign_in (default — use for 95% of tests)
# Sets tenant + subdomain context, then authenticates via Warden's login_as (no UI round-trip)
before do
  ActsAsTenant.current_tenant = organization
  fast_sign_in(user, organization)   # organization defaults to the default org
end

# 2. playwright_sign_in (ONLY when the auth UI itself is under test)
# Drives the real magic-link flow: submits the email form, extracts the magic link from
# ActionMailer::Base.deliveries, visits it, waits for the dashboard
playwright_sign_in(user, organization)

# Sign out through the UI when a scenario needs it
playwright_sign_out

Database and Transaction Management

Transactional Fixtures

By default, RSpec wraps each test in a database transaction and rolls it back:

# In spec/rails_helper.rb (default configuration)
RSpec.configure do |config|
  config.use_transactional_fixtures = true  # Default
end

# Each example gets a clean database
describe Widget do
  it 'creates a widget' do
    Widget.create(name: 'Test')
    expect(Widget.count).to eq(1)
  end

  it 'starts fresh' do
    expect(Widget.count).to eq(0)  # Previous widget was rolled back
  end
end

When to Disable Transactional Fixtures

# Disable for specific test that needs real commits
it 'tests background job processing', :no_transaction do
  # This test needs real database commits
end

# Or disable globally if using database_cleaner
RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

Data Setup Timing

# before(:example) - Runs inside transaction, auto-rollback
describe Widget do
  before(:example) do
    @widget = Widget.create(name: 'Test')
  end

  it 'uses the widget' do
    expect(@widget).to be_persisted
  end
  # @widget auto-deleted after test
end

# before(:context) - Runs OUTSIDE transaction, manual cleanup needed
describe Widget do
  before(:context) do
    @widget = Widget.create!(name: 'Shared')
  end

  after(:context) do
    @widget.destroy  # Must manually clean up!
  end

  it 'shares data across examples' do
    # @widget persists across all tests in this context
  end
end

Multi-Session Testing

Test multiple users simultaneously (e.g., real-time features):

describe 'Real-time collaboration' do
  it 'allows multiple users to edit simultaneously' do
    # Default session (User A)
    user_a = create(:user, name: 'Alice')
    sign_in user_a
    visit document_path(document)

    # Switch to User B's session
    Capybara.using_session('user_b') do
      user_b = create(:user, name: 'Bob')
      sign_in user_b
      visit document_path(document)

      fill_in 'Content', with: 'Bob was here'
      click_button 'Save'
    end

    # Back to User A's session (automatically)
    expect(page).to have_content('Bob was here', wait: 10)  # Real-time update
  end

  it 'switches between sessions explicitly' do
    # Create first session
    Capybara.using_session('admin') do
      admin = create(:user, :admin)
      sign_in admin
      visit admin_dashboard_path
    end

    # Create second session
    Capybara.using_session('client') do
      client = create(:user, :client)
      sign_in client
      visit client_portal_path
    end

    # Switch back to admin session
    Capybara.using_session('admin') do
      expect(page).to have_content('Admin Dashboard')
    end
  end
end

File Location Policy

All browser tests live under spec/system/<feature_area>/ with type: :system, js: true. Do NOT create spec/features/ files or use the feature/scenario DSL — feature specs are disallowed in this project (see CLAUDE.md's Request Specs Policy for the layer boundaries).

Factory Pattern Best Practices

# Use traits for common variations
let!(:staff_user) { create(:user, :staff) }
let!(:admin_user) { create(:user, :admin) }

# Build associations explicitly
let!(:client) { create(:client, organization: user.organization) }
let!(:appointment) { create(:appointment, client: client, user: user) }

# Use build_stubbed for non-persisted objects
let(:draft_document) { build_stubbed(:clinical_document) }

Test Organization

Group tests logically:

describe 'Creation workflow' do
  it 'creates new record with valid data' do
    # Happy path
  end

  it 'shows validation errors with invalid data' do
    # Validation testing
  end
end

describe 'Editing workflow' do
  let!(:existing_record) { create(:record) }

  it 'updates record with valid changes' do
    # Update happy path
  end
end

describe 'Authorization' do
  it 'prevents unauthorized access' do
    # Security testing
  end
end

Performance Considerations

# Use let! only for data needed in ALL tests
let!(:required_user) { create(:user) }

# Use let (lazy) for data needed in some tests
let(:optional_client) { create(:client) }

# Create minimum data needed
# Bad: Create 50 clients when testing one
# Good: Create only the clients needed for the test

# Use build instead of create when persistence not needed
let(:new_client) { build(:client) }

Turbo-Specific Testing

# Testing Turbo Frame updates
click_link 'Edit', href: /edit/ # Trigger frame navigation

within 'turbo-frame#edit-form' do
  fill_in 'Name', with: 'Updated'
  click_button 'Save'
end

# Wait for frame to update
expect(page).to have_css('turbo-frame#edit-form', text: 'Updated', wait: 5)

# Testing Turbo Stream responses
click_button 'Delete'

# Verify element removed via Turbo Stream
expect(page).to have_no_css("#item-#{item.id}", wait: 5)

Responsive and Mobile Testing

# Test mobile sidebar behavior
it 'shows mobile navigation on small screens', driver: :selenium_chrome_headless do
  # Set mobile viewport
  page.driver.browser.manage.window.resize_to(375, 667)

  visit page_path

  # Mobile toggle should be visible
  expect(page).to have_css('[data-responsive-sidebar-target="toggle"]', visible: true)

  # Sidebar should be hidden initially
  expect(page).to have_css('[aria-hidden="true"][role="navigation"]')

  # Click toggle
  find('[data-responsive-sidebar-target="toggle"]').click

  # Sidebar should be visible
  expect(page).to have_css('[aria-hidden="false"][role="navigation"]', wait: 5)
end

Accessibility Testing

# Verify ARIA attributes
button = find('button[aria-expanded]')
expect(button['aria-expanded']).to eq('false')

button.click

expect(button['aria-expanded']).to eq('true')

# Check for labels
expect(page).to have_css('label[for="field-id"]')
expect(page).to have_css('input#field-id')

# Verify roles
expect(page).to have_css('[role="navigation"]')
expect(page).to have_css('[role="main"]')

When to Write System Tests vs Other Tests

Write System Tests For:

  • Complete user workflows (sign up → create → edit → delete)
  • JavaScript interactions (Stimulus controllers, dynamic forms)
  • Multi-page flows (wizards, checkout processes)
  • Authentication and authorization flows
  • Real-world scenarios users will encounter

Don't Write System Tests For:

  • Model validations (use model specs)
  • Service object logic (use service specs)
  • Simple helper methods (use helper specs)
  • Edge cases better covered by unit tests

Checklist for Every System Test

  • Test has type: :system, js: true metadata and lives under spec/system/
  • Uses let! only for data every example needs; lazy let otherwise
  • Sets tenant context AND authenticates via fast_sign_in(user, organization) in before
  • Waits for Turbo/Stimulus after interactions
  • Uses semantic selectors (text, labels, test IDs)
  • Verifies success/error states explicitly
  • Handles async operations with proper waits
  • Tests one workflow per test (not too broad)
  • Has clear test description (references the plan's SC-NNN id when working from a plan)
  • Stubs every external integration (Stripe, RingRx, Stedi, Google, Vertex AI, Mailgun)
  • Passes with CI=true bundle exec rspec <file> (no retries)

Execution Strategy

When asked to write or debug system tests:

  1. Understand the feature: Read related controllers, views, and Stimulus controllers
  2. Identify workflows: Map out the user journey step by step
  3. Write setup code: Create necessary factories and data
  4. Write test incrementally: One interaction at a time, run frequently
  5. Add waits proactively: Don't wait for flakiness to appear
  6. Verify thoroughly: Check success states, error states, and edge cases
  7. Run multiple times: Execute test 5-10 times to verify stability
  8. Document complex patterns: Add comments for unusual waits or interactions

Example: Complete Reliable Test

require 'rails_helper'

RSpec.describe 'Client appointment booking', type: :system, js: true do
  let(:organization) { $default_organization }
  let!(:staff_user) { create(:user, :staff, organization: organization) }
  let!(:client) { create(:client, organization: organization) }

  before do
    ActsAsTenant.current_tenant = organization
    fast_sign_in(staff_user, organization)
  end

  it 'creates appointment and sends confirmation' do
    visit client_path(client)

    # Wait for page to load completely
    expect(page).to have_content(client.full_name, wait: 5)

    click_link 'Schedule Appointment'

    # Wait for Turbo navigation to appointment form
    expect(page).to have_current_path(new_client_appointment_path(client), wait: 10)

    # Wait for Stimulus controller if date picker uses one
    expect(page).to have_css("[data-controller*='datepicker']", wait: 5)

    # Fill form completely
    fill_in 'Date', with: 1.week.from_now.to_date
    fill_in 'Time', with: '10:00 AM'
    select 'Initial Consultation', from: 'Appointment Type'
    fill_in 'Notes', with: 'First appointment with new client'

    # Submit and wait for Turbo to complete
    click_button 'Create Appointment'

    # Verify success - wait for redirect
    expect(page).to have_current_path(client_path(client), wait: 10)
    expect(page).to have_content('Appointment created successfully', wait: 5)

    # Verify appointment appears in list
    within '[data-testid="appointments-list"]' do
      expect(page).to have_content('Initial Consultation')
      expect(page).to have_content(1.week.from_now.strftime('%B %d, %Y'))
    end

    # Verify database state
    expect(client.appointments.count).to eq(1)
    expect(client.appointments.last.appointment_type).to eq('initial_consultation')
  end
end

Remember: Reliability comes from understanding timing, proper waits, and semantic selectors. Never accept flaky tests - always investigate and fix the root cause.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.