Principle:Teamcapybara Capybara Driver Registration
| Knowledge Sources | |
|---|---|
| Domains | Testing, Driver_Architecture |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A registration pattern that associates a symbolic name with a factory block for lazily constructing browser driver instances.
Description
Driver Registration is the mechanism by which Capybara's pluggable driver architecture works. Rather than instantiating drivers eagerly, each driver is registered as a named block (factory) that receives the Rack application and returns a Capybara::Driver::Base subclass instance. Drivers are stored in a RegistrationContainer and instantiated lazily when a session first uses them.
This pattern enables:
- Pluggability — Any driver can be registered by name without modifying Capybara's core
- Lazy instantiation — Browsers are only launched when tests actually need them
- Per-test configuration — Different tests can use different driver configurations by name
Usage
Use Capybara.register_driver to define custom driver configurations. This is typically done in test setup (spec_helper.rb) before any tests run.
Theoretical Basis
# Abstract registration pattern (not actual code)
register_driver(:name) { |app| DriverClass.new(app, **options) }
# Later, when session needs the driver:
driver_instance = drivers[:name].call(rack_app)