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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/capybara-lockstep/capybara_ext.rb', line 125

def synchronize_around_script_method(meth)
  mod = Module.new do
    define_method meth do |script, *args, &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, &block)
    ensure
      if !Lockstep.synchronizing?
        # We haven't yet synchronized with whatever changes the JavaScript
        # did on the frontend.
        Lockstep.unsynchronize
      end
    end

    ruby2_keywords meth
  end
  prepend(mod)
end