module Capybara::Lockstep::SynchronizeAroundScriptMethod

Public Instance Methods

synchronize_around_script_method(meth) click to toggle source
Calls superclass method
# File lib/capybara-lockstep/capybara_ext.rb, line 44
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).tap do
        if !Lockstep.synchronizing?
          # We haven't yet synchronized with whatever changes the JavaScript
          # did on the frontend.
          Lockstep.synchronized = false
        end
      end
    end
    ruby2_keywords meth
  end
  prepend(mod)
end