Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Teamcapybara Capybara Selenium Driver Specialization

From Leeroopedia
Knowledge Sources
Domains Testing, Driver_Architecture
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for extending the Selenium driver with browser-specific behavior provided by Capybara::Selenium::Driver.register_specialization and the specialization modules.

Description

Driver.register_specialization(browser_name, module) stores a mapping from browser Symbol to a Ruby module. When Driver#browser is first accessed, after creating the Selenium WebDriver instance, specialize_driver looks up the browser name and calls extend(specialization_module) on the driver. This dynamically overrides methods like reset!, fullscreen_window, resize_window_to, and build_node.

The ChromeDriver specialization uses execute_cdp for CDP-based storage clearing and builds ChromeNode instances that have Chrome-specific file upload and drag-and-drop support.

Usage

Automatic at driver initialization. Register custom specializations for non-standard browsers.

Code Reference

Source Location

  • Repository: capybara
  • File: lib/capybara/selenium/driver.rb (L57-60), lib/capybara/selenium/driver_specializations/chrome_driver.rb (L1-117)

Signature

# Class method for registration
def self.register_specialization(browser_name, specialization)
  # @param browser_name [Symbol] e.g., :chrome, :firefox, :edge, :safari
  # @param specialization [Module] Module to extend the driver with
  @specializations ||= {}
  @specializations[browser_name] = specialization
end

# ChromeDriver module (example specialization)
module Capybara::Selenium::ChromeDriver
  def reset!
    # Uses CDP Storage.clearDataForOrigin and Network.clearBrowserCache
    super
  end

  def build_node(native_node, initial_cache = {})
    ::Capybara::Selenium::ChromeNode.new(self, native_node, initial_cache)
  end
end

Import

require 'capybara/selenium/driver'
# Specializations auto-loaded

I/O Contract

Inputs

Name Type Required Description
browser_name Symbol Yes Browser identifier (:chrome, :firefox, :edge, :safari)
specialization Module Yes Ruby module with browser-specific method overrides

Outputs

Name Type Description
Extended driver Capybara::Selenium::Driver Driver instance with browser-specific behavior mixed in

Usage Examples

Register Custom Specialization

module MyBrowserSpecialization
  def reset!
    # Custom reset behavior
    super
  end

  def build_node(native_node, initial_cache = {})
    MyCustomNode.new(self, native_node, initial_cache)
  end
end

Capybara::Selenium::Driver.register_specialization(
  :my_browser,
  MyBrowserSpecialization
)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment