Ruby on rails 为什么我的Capybara特性测试在等待Ajax时会间歇性失败?

Ruby on rails 为什么我的Capybara特性测试在等待Ajax时会间歇性失败?,ruby-on-rails,ajax,ruby-on-rails-4,capybara,Ruby On Rails,Ajax,Ruby On Rails 4,Capybara,我使用的是Capybara 2.13.0,我得到了一个测试,该测试依赖于向DOM中添加动态内容的Ajax请求 test 'contact data becomes visible when providing the correct password' do login_as_researcher visit '/participants/3' assert page.has_css?("ul#redacted-contact-data") click_button('Unloc

我使用的是Capybara 2.13.0,我得到了一个测试,该测试依赖于向DOM中添加动态内容的Ajax请求

test 'contact data becomes visible when providing the correct password' do
  login_as_researcher
  visit '/participants/3'
  assert page.has_css?("ul#redacted-contact-data")
  click_button('Unlock Contact Data')
  # Use the correct password
  fill_in('enter your password', with: 'password1')

  # Ajax happens here. It renders a partial and places it in the dom.
  click_button('Submit')
  # Test sometimes fails here..
  assert page.has_css?("ul#contact-data"), -> { page.body }

end
我读过很多文章,说Capybara自动等待元素出现在HTML中,没有必要再手动告诉Capybara等待,但我的案例中没有这种情况

75%的时间此测试通过,当测试失败且页面被转储时,它包含JS响应中呈现的部分内容,而不是完整的HTML页面

知道这是怎么回事吗?我只能假设这是由于服务器每次响应Ajax请求所需的时间,而在某些情况下,水豚等待的时间不够长

我已经在测试助手中设置了
Capybara.default\u max\u wait\u time=5

我试过selenium和poltergeist两种车手,但没什么区别

下面是
test.log

Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
  Researcher Load (0.3ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Redirected to http://www.example.com/session/new
Filter chain halted as :researcher_only rendered or redirected
Completed 302 Found in 30ms (ActiveRecord: 0.3ms)
Started GET "/session/new" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#new as HTML
  Rendered sessions/new.html.erb within layouts/application (9.5ms)
  Researcher Load (0.1ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Completed 200 OK in 248ms (Views: 247.6ms | ActiveRecord: 0.1ms)
Started POST "/session" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "username"=>"nick1", "password"=>"[FILTERED]", "commit"=>"Enter"}
  Researcher Load (0.1ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."username" = ? LIMIT 1  [["username", "nick1"]]
Redirected to http://www.example.com/
Completed 302 Found in 78ms (ActiveRecord: 0.1ms)
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
  Researcher Load (0.1ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1  [["id", 2]]
  Rendered home/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 10ms (Views: 8.6ms | ActiveRecord: 0.1ms)
Started GET "/participants/3" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by ParticipantsController#show as HTML
  Parameters: {"id"=>"3"}
  Participant Load (1.9ms)  SELECT  "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1  [["id", 3]]
  Researcher Load (0.1ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1  [["id", 2]]
  ContactData Load (0.2ms)  SELECT  "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1  [["participant_id", 3]]
  AlcoholService Load (0.1ms)  SELECT  "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1  [["id", 16]]
  Baseline Load (0.3ms)  SELECT  "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1  [["participant_id", 3]]
  Rendered participants/_unlock_contact_data.html.erb (1.1ms)
  Rendered participants/_details.html.erb (52.8ms)
  Rendered participants/show.html.erb within layouts/application (58.8ms)
Completed 200 OK in 94ms (Views: 69.9ms | ActiveRecord: 3.0ms)
Started POST "/participants/unlock_contact_data?participant_id=3" for 127.0.0.1 at 2017-05-01 18:09:24 +0100
Processing by ParticipantsController#unlock_contact_data as JS
  Parameters: {"utf8"=>"✓", "from"=>"show", "password"=>"[FILTERED]", "commit"=>"Submit", "participant_id"=>"3"}
  Researcher Load (0.1ms)  SELECT  "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1  [["id", 2]]
  Participant Load (0.0ms)  SELECT  "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1  [["id", 3]]
  ContactData Load (0.1ms)  SELECT  "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1  [["participant_id", 3]]
  AlcoholService Load (0.1ms)  SELECT  "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1  [["id", 16]]
  Baseline Load (0.0ms)  SELECT  "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1  [["participant_id", 3]]
  Rendered participants/_details.html.erb (4.6ms)
  Rendered participants/unlock_contact_data.js.erb (6.7ms)
Completed 200 OK in 81ms (Views: 10.8ms | ActiveRecord: 0.3ms)
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction

有什么我可能遗漏的吗?

尝试将
默认值\u max\u wait\u time
设置为30左右-这将决定页面是否需要一段时间才能呈现。此外,在失败后查看test.log,查看实际发出了什么请求以及浏览器响应了什么(检查它是否确实响应了:js响应)。另外,您不应该断言
has\u css?
,因为错误消息是无用的。看起来您可能正在使用minitest,因此,请参阅并使用
assert\u css
非常感谢您的回复。我已经将
default\u max\u wait\u time
增加到了30,而且这种情况仍在发生。服务器每次肯定都会用JS响应进行响应。我注意到的一个奇怪的观察结果是,如果我注释掉这个文件中的所有其他测试,并且只运行这个测试,它几乎每次都失败。如果有更多的测试在运行,那么它通过测试的几率就更高。我是否可能需要使用Rspec并执行类似于
page.should_css(“ul#contact data”)
的操作?从minitest到Rspec的交换不会有什么区别,
assert_css
have_css?
have_css
方法基本上都会调用相同的例程来检查数据。使用其中一种只是个人喜好。您的
login\u as\u research
定义为什么(您确定用户每次都在登录吗)?只运行一个测试,并将失败的test.log的相关部分添加到问题中。还要注意,您编写断言的方式实际上并没有在水豚等待结束时向您显示页面内容,因为您在它检查/重新检查内容之前就得到了正文。它们应该写在
assert page.has#css?(“ul#contact data”),->{page.body}
上,所以在断言失败之前,
page.body
不会得到评估。谢谢,我在问题中添加了
test.log
输出。