Skip to content

Instantly share code, notes, and snippets.

@passalini
Last active April 7, 2017 14:08
Show Gist options
  • Save passalini/33d26dd1fa00044caa26 to your computer and use it in GitHub Desktop.
Save passalini/33d26dd1fa00044caa26 to your computer and use it in GitHub Desktop.

Revisions

  1. passalini revised this gist Nov 19, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions wait_for_ajax.rb
    Original file line number Diff line number Diff line change
    @@ -31,8 +31,10 @@ def click_ajax_link_or_button(locator, options = {})

    wait_for_ajax
    end

    def wait_for_ajax

    def wait_for_ajax(&block)
    block.call if block

    Timeout.timeout(Capybara.default_wait_time) do
    loop do
    sleep 0.1
  2. passalini created this gist Nov 4, 2014.
    51 changes: 51 additions & 0 deletions wait_for_ajax.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # Ajax testing with ruby and capybara
    #
    # Add this to spec/support
    #
    # When a link or button starts an ajax request, instead of use Capybara
    # click_link, click_button and click_link_or_button methods use click_ajax_link,
    # click_ajax_button and click_ajax_link_or_button methods. You can still use
    # capybara methods and right after it, call wait_for_ajax method.
    #
    # This methods will wait until Capybara.default_wait_time for the ajax request
    # to finish before continue the normal tests flow.
    #

    require 'timeout'

    module WaitForAjax
    def click_ajax_link(locator, options = {})
    click_link(locator, options)

    wait_for_ajax
    end

    def click_ajax_button(locator, options = {})
    click_button(locator, options)

    wait_for_ajax
    end

    def click_ajax_link_or_button(locator, options = {})
    click_link_or_button(locator, options)

    wait_for_ajax
    end

    def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
    loop do
    sleep 0.1
    break if finished_all_ajax_requests?
    end
    end
    end

    def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
    end
    end

    RSpec.configure do |config|
    config.include WaitForAjax
    end