Sha256: c3287c0b14e77439adecb64ef2140e9b399ead1602f80bec8b2abae8320f0277

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Prickle
  module Capybara
    module Popups
      class Webkit

        include ::Capybara::DSL

        def confirm &block
          set_type_to :confirm
          manage_popup true, &block
        end

        def dismiss &block
          set_type_to :confirm
          manage_popup false, &block
        end

        def accept &block
          set_type_to :alert
          manage_popup true, &block
        end

        def message
          @message
        end

        def contains_message? message
          raise Exceptions::MessageNotContainedInPopup.new(self.message) unless self.message.include? message
        end

        private

        def set_type_to type
          @type = type
        end

        def type
          @type.to_s
        end

        def manage_popup accept
          listen_and accept
          yield
          restore
          capture_message
          self
        end

        def listen_and accept
          page.execute_script %{
          window.original_#{type}_function = window.#{type}
          window.#{type}_msg = null
          window.#{type} = function(msg) { window.#{type}_msg = msg; return #{!!accept}; }
          }
        end

        def restore
          page.execute_script "window.#{type} = window.original_#{type}_function"
        end

        def capture_message
          @message = page.evaluate_script "window.#{type}_msg"
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prickle-0.1.0 lib/prickle/capybara/popup/webkit.rb
prickle-0.0.6 lib/prickle/capybara/popup/webkit.rb