Skip to content

Instantly share code, notes, and snippets.

@meltedice
Created July 22, 2016 02:52
Show Gist options
  • Save meltedice/3fdce0299d9ead4b17258b5dbfc1f64c to your computer and use it in GitHub Desktop.
Save meltedice/3fdce0299d9ead4b17258b5dbfc1f64c to your computer and use it in GitHub Desktop.
Wait for dynamic content with capybara feature spec
# wait_for_content 'Submit'
# wait_for_content 'Submit', selector: '.super-button'
# wait_for_content 'Submit', selector: 'form .super-button'
# wait_for_content 'Message', timeout_s: 5
# wait_for_content 'Messages?', timeout_s: 5
# wait_for_content '[ck]lass(es)?'
# wait_for_content 'Thanks' do
# puts 'Waiting...'
# end
#
def wait_for_content(content_js_regexp, options={}, &block)
begin
page.document.synchronize(options[:timeout_s] || Capybara.default_wait_time) do
selector = options[:selector] || 'html'
js_code = "(new RegExp('#{content_js_regexp}')).test($('#{selector}').html())"
found = page.evaluate_script(js_code)
if found
true
else
yield if block_given?
false
end
end
rescue Timeout::Error => e
puts "---------------------------------------- #wait_for_content: #{content_js_regexp}"
puts "---------------------------------------- e: Timeout::Error"
puts e.inspect rescue nil
puts "---------------------------------------- page.response_headers:"
puts page.response_headers rescue nil
puts "---------------------------------------- page.text:"
puts page.text rescue nil
puts "---------------------------------------- page.html:"
puts page.html rescue nil
save_page_response = save_page rescue nil
puts "---------------------------------------- save_page: #{save_page_response}"
puts "---------------------------------------- //"
raise
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment