简体   繁体   中英

Capybara wait ajax to load

Hi I have error in my capybara testing env

 unknown error: jQuery is not defined
        (Session info: chrome=43.0.2357.125)

I think it's related to ajax wait function

 def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      element = false
      until element do
        begin
          sleep(0.05)
          count = 0
          begin
            element = page.evaluate_script('jQuery.active') == 0
          rescue Errno::ECONNRESET => e
            count += 1
            restart_phantomjs
            retry unless count > RETRY_COUNT_ERROR_CONNECT
          rescue Capybara::Poltergeist::DeadClient
            count += 1
            restart_phantomjs
            retry unless count > RETRY_COUNT_ERROR_CONNECT
          rescue Capybara::Poltergeist::JavascriptError
          end
        end
      end
    end
  end

Any idea how to solve this? Any help is welcomed. Maybe updating chrome driver is required, not sure whats going on.

The wait_for_ajax method you show is specifically written to be used with Poltergeist and requires jQuery to be loaded in the page (the Capybara::Poltergeist error rescuing, restart_phantomjs, etc) so you should not be using it with what appears to be selenium-webdriver using Chrome.

There's also the fact that wait_for_ajax shouldn't be needed 99.9% of the time with Capybara 2.1+, when used correctly, since it will automatically wait for elements/text to appear so whether it's coming from an ajax request or a page load shouldn't matter. That combined with the fact that more and more libraries are using XMLHttpRequest directly rather that through jQuery means wait_for_ajax is virtually unnecessary. Pretty much the only exception to that is when you have a badly designed UI that provides no feedback to the user about background requests and their effects, in which case I'd suggest fixing your UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM