Does Ruby on Rails Work With Cypress?
Cypress works excellently with Rails as a frontend testing tool, testing your Rails app as a black box through its browser interface.
Quick Facts
How Ruby on Rails Works With Cypress
Cypress and Rails are a natural pairing because they operate at different layers. Rails handles backend logic and server-side rendering, while Cypress tests the actual browser behavior of your Rails application. You run your Rails development server (rails s) in one terminal, then point Cypress at localhost:3000 to test your application end-to-end. Cypress doesn't need Rails-specific plugins—it simply loads your Rails app in a real browser and interacts with it like a user would.
The developer experience is smooth: Rails conventions don't interfere with Cypress at all. You write test specs in JavaScript/TypeScript, use Cypress's built-in selector engine to find Rails-rendered elements, and Cypress's time-travel debugging lets you inspect network requests and DOM state after each action. Rails' asset pipeline and view rendering happen normally—Cypress just tests the final HTML output. The main consideration is ensuring your test database is properly isolated; use database_cleaner or Rails' transactional fixtures to keep tests fast and independent.
One architectural note: Cypress runs in the browser context, so it can't directly access Rails models or database fixtures. For complex setup, either use database factories (FactoryBot) seeded before tests, or leverage Cypress's cy.request() to hit Rails API endpoints. This separation actually encourages better testing practices by forcing you to test user-facing behavior rather than implementation details.
Best Use Cases
Quick Setup
npm install --save-dev cypress && npx cypress open// cypress/e2e/rails_app.cy.js
describe('Rails App E2E Tests', () => {
beforeEach(() => {
cy.visit('http://localhost:3000')
})
it('creates a new post', () => {
cy.get('a[href="/posts/new"]').click()
cy.get('input[name="post[title]"]').type('My First Post')
cy.get('textarea[name="post[body]"]').type('This is the content')
cy.get('button[type="submit"]').click()
cy.contains('Post was successfully created').should('be.visible')
cy.url().should('include', '/posts/')
})
it('displays posts on homepage', () => {
cy.get('article').should('have.length.greaterThan', 0)
cy.get('article').first().within(() => {
cy.get('h2').should('exist')
cy.get('p').should('be.visible')
})
})
})
Known Issues & Gotchas
Cypress runs in browser context and cannot access Rails console or models directly
Fix: Use FactoryBot factories with database seeding or create test data via Rails API endpoints before running tests
CSRF token mismatches in form tests when Rails has strict CSRF protection enabled
Fix: Disable CSRF protection in test environment or configure Cypress to extract and send CSRF tokens from pages
Test database not properly isolated, causing flaky tests and data pollution between test runs
Fix: Configure database_cleaner gem with Cypress hooks or use transactional test fixtures with spec_helper
Slow test execution when Rails app takes time to boot or database queries are slow
Fix: Use parallel test runners, optimize fixture loading, and leverage Cypress's caching for asset requests
Alternatives
- •Selenium with Ruby + Rails (Capybara gem) - mature Ruby-native solution with WebDriver support
- •Playwright with TypeScript - cross-browser testing with better performance than Cypress
- •RSpec with Capybara - Rails-integrated testing framework using headless browsers or real browsers
Resources
Related Compatibility Guides
Explore more compatibility guides