Module: Capybara::Presenter::CapybaraExtensions

Included in:
InstanceMethods
Defined in:
lib/capybara/presenter/capybara_extensions.rb

Overview

Extends Capybara methods with presentation features. Wraps common Capybara actions to add delays and logging for better presentations.

Constant Summary collapse

WRAPPED_METHODS =
%i[visit click_button click_link click_on fill_in select choose check uncheck
attach_file].freeze

Instance Method Summary collapse

Instance Method Details

#setup_presenter_delaysObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/capybara/presenter/capybara_extensions.rb', line 12

def setup_presenter_delays
  return unless presenter_mode?
  return if self.class.instance_variable_get(:@presenter_delays_setup)

  self.class.instance_variable_set(:@presenter_delays_setup, true)

  WRAPPED_METHODS.each do |action|
    next if respond_to?(:"#{action}_without_presenter")

    # Create alias for original method
    self.class.alias_method :"#{action}_without_presenter", action

    # Override the method with presenter delays
    self.class.define_method(action) do |*args, **kwargs, &block|
      if presenter_mode?
        log_presenter_action(action, args.first)

        # Pre-action delay
        case action
        when :click_button, :click_link, :click_on
          presenter_delay(0.3)
        when :fill_in, :select, :choose, :check, :uncheck
          presenter_delay(0.2)
        when :visit
          presenter_delay(0.5)
        when :attach_file
          presenter_delay(0.3)
        end
      end

      # Execute the original action
      result = send(:"#{action}_without_presenter", *args, **kwargs, &block)

      # Post-action delay for actions that trigger page changes
      presenter_delay(0.8) if presenter_mode? && %i[click_button click_link click_on visit].include?(action)

      result
    end
  end
end