Module: Capybara::Lockstep::SynchronizeAroundScriptMethod

Defined in:
lib/capybara-lockstep/capybara_ext.rb

Instance Method Summary collapse

Instance Method Details

#synchronize_around_script_method(meth) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/capybara-lockstep/capybara_ext.rb', line 118

def synchronize_around_script_method(meth)
  mod = Module.new do
    define_method meth do |script, *args, **kwargs, &block|
      # Synchronization uses execute_script itself, so don't synchronize when
      # we're already synchronizing.
      if !Lockstep.synchronizing?
        # It's generally a good idea to synchronize before a JavaScript wants
        # to access or observe an earlier state change.
        #
        # In case the given script navigates away (with `location.href = url`,
        # `history.back()`, etc.) we would kill all in-flight requests. For this case
        # we force a non-lazy synchronization so we pick up all client-side changes
        # that have not been caused by Capybara commands.
        script_may_navigate_away = script =~ /\b(location|history)\b/
        Lockstep.auto_synchronize(lazy: !script_may_navigate_away, log: "Synchronizing before script: #{script}")
      end

      super(script, *args, **kwargs, &block)
    ensure
      if !Lockstep.synchronizing?
        # We haven't yet synchronized with whatever changes the JavaScript
        # did on the frontend.
        Lockstep.unsynchronize
      end
    end
  end
  prepend(mod)
end