Skip to content

Instantly share code, notes, and snippets.

@endymion
Forked from metaskills/wait_until.rb
Created March 9, 2012 17:59
Show Gist options
  • Select an option

  • Save endymion/2007777 to your computer and use it in GitHub Desktop.

Select an option

Save endymion/2007777 to your computer and use it in GitHub Desktop.

Revisions

  1. endymion revised this gist Mar 9, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -54,3 +54,8 @@ def wait_for_page_load
    wait_for_loading && wait_for_loaded
    end

    def wait_for_transition_to_complete
    wait_until(5) do
    not page.evaluate_script('$("body").hasClass("ui-mobile-viewport-transitioning");')
    end
    end
  2. @metaskills metaskills revised this gist Sep 21, 2011. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -35,3 +35,22 @@ def assert_modal_hidden
    rescue Capybara::TimeoutError
    flunk 'Expected modal to be hidden.'
    end

    # Examples of waiting for a page loading to show and hide in jQuery Mobile.

    def wait_for_loading
    wait_until { page.find('html')[:class].include?('ui-loading') }
    rescue Capybara::TimeoutError
    flunk "Failed at waiting for loading to appear."
    end

    def wait_for_loaded
    wait_until { !page.find('html')[:class].include?('ui-loading') }
    rescue Capybara::TimeoutError
    flunk "Failed at waiting for loading to complete."
    end

    def wait_for_page_load
    wait_for_loading && wait_for_loaded
    end

  3. @metaskills metaskills revised this gist Aug 27, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,12 @@ def modal_wrapper_id

    def assert_modal_visible
    wait_until { page.find(modal_wrapper_id).visible? }
    assert page.find(modal_wrapper_id).visible?, 'Expected modal to be visible.'
    rescue Capybara::TimeoutError
    flunk 'Expected modal to be visible.'
    end

    def assert_modal_hidden
    wait_until { !page.find(modal_wrapper_id).visible? }
    refute page.find(modal_wrapper_id).visible?, 'Expected modal to be hidden.'
    rescue Capybara::TimeoutError
    flunk 'Expected modal to be hidden.'
    end
  4. @metaskills metaskills revised this gist Aug 26, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    describe 'Modal' do

    should 'display login errors ' do
    should 'display login errors' do
    visit root_path
    click_link 'My HomeMarks'
    within '#login_area' do
    @@ -12,7 +12,7 @@
    end
    # DO NOT sleep(1) HERE!
    assert_modal_visible
    # ...
    page.find(modal_wrapper_id).text.must_match %r{login failed.*use the forgot password}i
    end

    end
  5. @metaskills metaskills revised this gist Aug 26, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@
    within '#login_area' do
    fill_in 'email', with: '[email protected]'
    fill_in 'password', with: 'test'
    # DO NOT sleep(1) HERE!
    click_button 'Login'
    end
    # DO NOT sleep(1) HERE!
    assert_modal_visible
    # ...
    end
  6. @metaskills metaskills revised this gist Aug 26, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    within '#login_area' do
    fill_in 'email', with: '[email protected]'
    fill_in 'password', with: 'test'
    # DO NOT sleep(1) HERE!
    click_button 'Login'
    end
    assert_modal_visible
  7. @metaskills metaskills created this gist Aug 26, 2011.
    34 changes: 34 additions & 0 deletions wait_until.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Have you ever had to sleep() in Capybara-WebKit to wait for AJAX and/or CSS animations?

    describe 'Modal' do

    should 'display login errors ' do
    visit root_path
    click_link 'My HomeMarks'
    within '#login_area' do
    fill_in 'email', with: '[email protected]'
    fill_in 'password', with: 'test'
    click_button 'Login'
    end
    assert_modal_visible
    # ...
    end

    end

    # Avoid it by using Capybara's #wait_until method. My modal visible/hidden helpers
    # do just that. The #wait_until uses the default timeout value.

    def modal_wrapper_id
    '#hmarks_modal_sheet_wrap'
    end

    def assert_modal_visible
    wait_until { page.find(modal_wrapper_id).visible? }
    assert page.find(modal_wrapper_id).visible?, 'Expected modal to be visible.'
    end

    def assert_modal_hidden
    wait_until { !page.find(modal_wrapper_id).visible? }
    refute page.find(modal_wrapper_id).visible?, 'Expected modal to be hidden.'
    end