Skip to content

Instantly share code, notes, and snippets.

@gpleiss
Last active August 29, 2015 14:14
Show Gist options
  • Save gpleiss/ff68ac8eeee6193e903d to your computer and use it in GitHub Desktop.
Save gpleiss/ff68ac8eeee6193e903d to your computer and use it in GitHub Desktop.
Example of Capybara `synchronize` in a matcher
def has_analytics_message?(msg)
document.synchronize do |variable|
gaq = evaluate_script('window._gaq')
raise Capybara::ElementNotFound, "#{msg} was not fired.\nActual analytics events triggered:\n\n#{gaq}" unless gaq.include?(msg)
end
true
end
@gpleiss
Copy link
Author

gpleiss commented Feb 6, 2015

This matcher is checking the window._gaq javascript variable to see if it has a value.

We need to wrap this in a synchronize block, because we want to wait until this javascript block becomes true.

The thing that always gets me: the synchronize block needs to throw a Capybara::ElementNotFound exception. When this exception is caught, it reruns the synchronize block until it's true, or until the default timeout is hit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment