Capybara function have_css is very nice but it is very limited. With this simple code I can replace it with jquery:

module JQueryMacro def jquery_element_count(jquery_query) page.driver.evaluate_script <<-EOS function() { return $("#{jquery_query}").size(); }(); EOS end def jquery_have_css(jquery_query) result = false Timeout.timeout(Capybara.default_wait_time) do while !result sleep 1 result = jquery_element_count(jquery_query) > 0 end end result end RSpec::Matchers.define :have_jquery_css do |jquery_query| match do jquery_have_css(jquery_query) end end end

Now i can execute queries like:

expect(page).to have_jquery_css(".result-suggestions li:nth-child(0):not(.selected)")

Comments