Sha256: 47dadf821f3bc0d9bd56aa00bdd989040eb6b3856726cab89448b503e1c8fa26

Contents?: true

Size: 1.75 KB

Versions: 28

Compression:

Stored size: 1.75 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 #{answer.to_json}; }"
      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

28 entries across 28 versions & 1 rubygems

Version Path
watir-webdriver-0.2.0.dev2 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.2.0.dev lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.9 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.8 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.7 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.6 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.5 lib/watir-webdriver/extensions/alerts.rb
watir-webdriver-0.1.4 lib/watir-webdriver/extensions/alerts.rb