Skip to content

Instantly share code, notes, and snippets.

@michaelglass
Last active September 24, 2019 10:15
Show Gist options
  • Select an option

  • Save michaelglass/8610317 to your computer and use it in GitHub Desktop.

Select an option

Save michaelglass/8610317 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelglass revised this gist Apr 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example_usage.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # this should catch the following alert
    #
    # <button onclick='alert('totally alerted')>make an alert happen!</button>
    # <button onclick="alert('totally alerted')">make an alert happen!</button>
    #

    AlertConfirmer.get_alert_text_from do
  2. michaelglass revised this gist Apr 29, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion example_usage.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # this should catch the following alert
    #
    # <button onclick='alert('totally alerted')>make an alert happen!</button>
    #

    AlertConfirmer.get_alert_text_from do
    click_buttton 'make an alert happen!'
    end.should == 'totally alerted'
    end.should == 'totally alerted'
  3. michaelglass created this gist Jan 25, 2014.
    55 changes: 55 additions & 0 deletions alerts_and_confirms.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    module AlertConfirmer
    class << self
    def reject_confirm_from &block
    handle_js_modal 'confirm', false, &block
    end

    def accept_confirm_from &block
    handle_js_modal 'confirm', true, &block
    end

    def accept_alert_from &block
    handle_js_modal 'alert', true, &block
    end

    def get_alert_text_from &block
    handle_js_modal 'alert', true, true, &block
    get_modal_text 'alert'
    end

    def get_modal_text(name)
    page.evaluate_script "window.#{name}Msg;"
    end

    private

    def handle_js_modal name, return_val, wait_for_call = false, &block
    modal_called = "window.#{name}.called"
    page.execute_script "
    window.original_#{name}_function = window.#{name};
    window.#{name} = function(msg) { window.#{name}Msg = msg; window.#{name}.called = true; return #{!!return_val}; };
    #{modal_called} = false;
    window.#{name}Msg = null;"

    block.call

    if wait_for_call
    timed_out = false
    timeout_after = Time.now + Capybara.default_wait_time
    loop do
    if page.evaluate_script(modal_called).nil?
    raise 'appears that page has changed since this method has been called, please assert on page before calling this'
    end

    break if page.evaluate_script(modal_called) ||
    (timed_out = Time.now > timeout_after)

    sleep 0.001
    end
    raise "#{name} should have been called" if timed_out
    end
    ensure
    page.execute_script "window.#{name} = window.original_#{name}_function"
    end
    end
    end
    3 changes: 3 additions & 0 deletions example_usage.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    AlertConfirmer.get_alert_text_from do
    click_buttton 'make an alert happen!'
    end.should == 'totally alerted'