Created
July 22, 2016 02:52
-
-
Save meltedice/3fdce0299d9ead4b17258b5dbfc1f64c to your computer and use it in GitHub Desktop.
Wait for dynamic content with capybara feature spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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