Back to Repositories

Testing Save and Open Page Debugging Workflow in Capybara

This test suite examines Capybara’s save_and_open_page functionality, which allows developers to debug their tests by saving and viewing the current state of the page. The specs verify the interaction between Capybara and the Launchy gem for opening saved HTML pages in a browser.

Test Coverage Overview

The test suite focuses on the save_and_open_page feature in Capybara, ensuring proper file handling and browser integration.

Key areas covered include:
  • Temporary file creation with HTML content
  • Launchy gem integration for browser opening
  • Cleanup of generated files
  • File naming pattern verification

Implementation Analysis

The testing approach uses RSpec mocking to verify the interaction between Capybara and Launchy without actually opening browsers during tests. The implementation leverages before/after hooks for setup and cleanup, with specific attention to file system operations and external dependencies.

Technical patterns include:
  • Mock expectations for Launchy.open
  • Regular expression matching for generated filenames
  • Automated cleanup of test artifacts

Technical Details

Testing tools and configuration:
  • RSpec for test framework
  • Launchy gem for browser interaction
  • FileUtils for file system operations
  • Directory glob patterns for file management
  • Regular expressions for filename validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby and Capybara development. It demonstrates proper test isolation through cleanup procedures, mock object usage for external dependencies, and clear test organization.

Notable practices include:
  • Proper test setup and teardown
  • External dependency mocking
  • Resource cleanup
  • Clear test case isolation

teamcapybara/capybara

lib/capybara/spec/session/save_and_open_page_spec.rb

            
# frozen_string_literal: true

require 'launchy'

Capybara::SpecHelper.spec '#save_and_open_page' do
  before do
    @session.visit '/foo'
  end

  after do
    Dir.glob('capybara-*.html').each do |file|
      FileUtils.rm(file)
    end
  end

  it 'sends open method to launchy' do
    allow(Launchy).to receive(:open)
    @session.save_and_open_page
    expect(Launchy).to have_received(:open).with(/capybara-\d+\.html/)
  end
end