Capybara没有等待ajax请求完成

时间:2021-06-30 20:55:45

I am trying to test selecting an option from select tag (these options are fetched from a remote database server). During normal interation with the website, it does not take more than a fraction of a second to populate this dropdown. However, when I run the following test,

我正在尝试测试从select标签中选择一个选项(这些选项是从远程数据库服务器获取的)。在与网站正常交互期间,填充此下拉列表的时间不会超过几分之一秒。但是,当我运行以下测试时,



    When /^(?:|I )select "([^"]*)" from "([^"]*)" in search form$/ do |value, field|
      within "#select_container" do
        save_and_open_page
        page.should have_css("#criteria_div_code > option:nth-child(10)")
        select(value, :from => field)
      end 
    end


I get the following error,

我收到以下错误,

expected css "#criteria_div_code > option:nth-child(10)" to return something (RSpec::Expectations::ExpectationNotMetError)

期望的css“#criteria_div_code>选项:nth-​​child(10)”返回一些东西(RSpec :: Expectations :: ExpectationNotMetError)

The dropdown is populated with at least 20 options and so I just test for the presence of the 10th option (for now).

下拉列表中至少包含20个选项,因此我只测试第10个选项的存在(暂时)。

save_and_open_page shows that only one option (default option) exists instead of at least 10 and hence the "ExpectionNotMetError" comes up.

save_and_open_page显示只存在一个选项(默认选项)而不是至少10个,因此出现“ExpectionNotMetError”。

Capybara.default_wait_time = 30 - Ample time for the lists to get populated.

Capybara.default_wait_time = 30 - 填充列表的充足时间。

Isn't capybara waiting for the ajax call to finish?

是不是水豚等待ajax电话完成?

Am I missing something here?

我在这里错过了什么吗?

2 个解决方案

#1


2  

You might want to check my response to setting timeouts for ajax resynchronization Using Capybara for AJAX integration tests. Resynchronization timeout defaults to 10secs and if your response does not return before that time, you will not get any responses especially if you have set :resynchronize to false in your configurations. below is a snippet to set that timeout

您可能想检查我对ajax重新同步设置超时的响应使用Capybara进行AJAX集成测试。重新同步超时默认为10秒,如果您的响应在此之前未返回,则不会得到任何响应,尤其是在配置中将resynchronize设置为false时。下面是设置超时的片段

 Capybara.register_driver :selenium do |app|
   Capybara::Selenium::Driver.new(app, :browser => :firefox, :resynchronization_timeout => 1000)
 end

NOTE: if you previously set :resynchronize to false, you need to set this to true.

注意:如果您之前设置:resynchronize为false,则需要将其设置为true。

#2


0  

I guess you need to user js driver for ajax testing,

我猜你需要用js驱动程序进行ajax测试,

describe 'some stuff which requires js', :js => true do
  it 'will use the default js driver'
  it 'will switch to one specific driver', :driver => :celerity
end

Also note the following line - Capybara can block and wait for Ajax requests to finish after you’ve interacted with the page. To enable this behaviour, set the :resynchronize driver option to true.

另请注意以下行 - Capybara可以在您与页面交互后阻止并等待Ajax请求完成。要启用此行为,请将:resynchronize驱动程序选项设置为true。

#1


2  

You might want to check my response to setting timeouts for ajax resynchronization Using Capybara for AJAX integration tests. Resynchronization timeout defaults to 10secs and if your response does not return before that time, you will not get any responses especially if you have set :resynchronize to false in your configurations. below is a snippet to set that timeout

您可能想检查我对ajax重新同步设置超时的响应使用Capybara进行AJAX集成测试。重新同步超时默认为10秒,如果您的响应在此之前未返回,则不会得到任何响应,尤其是在配置中将resynchronize设置为false时。下面是设置超时的片段

 Capybara.register_driver :selenium do |app|
   Capybara::Selenium::Driver.new(app, :browser => :firefox, :resynchronization_timeout => 1000)
 end

NOTE: if you previously set :resynchronize to false, you need to set this to true.

注意:如果您之前设置:resynchronize为false,则需要将其设置为true。

#2


0  

I guess you need to user js driver for ajax testing,

我猜你需要用js驱动程序进行ajax测试,

describe 'some stuff which requires js', :js => true do
  it 'will use the default js driver'
  it 'will switch to one specific driver', :driver => :celerity
end

Also note the following line - Capybara can block and wait for Ajax requests to finish after you’ve interacted with the page. To enable this behaviour, set the :resynchronize driver option to true.

另请注意以下行 - Capybara可以在您与页面交互后阻止并等待Ajax请求完成。要启用此行为,请将:resynchronize驱动程序选项设置为true。