Sha256: 43fa8111631076781fe1d464ea34a5d52770d0c2e4ab67e76b0564380a2f6e30

Contents?: true

Size: 1.76 KB

Versions: 11

Compression:

Stored size: 1.76 KB

Contents

module Watir

  #
  # Module provided by optional require:
  #
  #   require "watir-webdriver/extensions/alerts"
  #

  module AlertHelper

    #
    # Overwrite window.alert()
    #
    # This method is provided by an optional require - API is subject to change.
    #
    # @example
    #   browser.alert do
    #     browser.button(:value => "Alert").click
    #   end #=> "the alert message"
    #

    def alert(&blk)
      execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
      yield
      execute_script "return window.__lastWatirAlert"
    end

    #
    # Overwrite window.confirm()
    #
    # This method is provided by an optional require - API is subject to change.
    #
    # @example
    #   browser.confirm(true) do
    #     browser.button(:value => "Confirm").click
    #   end #=> "the confirm message"

    def confirm(bool, &blk)
      execute_script "window.confirm = function(msg) { window.__lastWatirConfirm = msg; return #{!!bool} }"
      yield
      execute_script "return window.__lastWatirConfirm"
    end

    #
    # Overwrite window.prompt()
    #
    # This method is provided by an optional require - API is subject to change.
    #
    # @example
    #   browser.prompt("hello") do
    #     browser.button(:value => "Prompt").click
    #   end #=> { :message => "foo", :default_value => "bar" }
    #

    def prompt(answer, &blk)
      execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{MultiJson.encode answer}; }"
      yield
      result = execute_script "return window.__lastWatirPrompt"

      result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k)}
      result
    end
  end

  class Browser
    include AlertHelper
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
watir-webdriver-0.5.8 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.7 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.6 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.5 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.4 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.3 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.2 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.1 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.5.0 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.4.1 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.4.0 lib/watir-webdriver/extensions/alerts.rb