为什么我的Capybara功能测试在等待Ajax时会间歇性地失败?

时间:2017-05-01 15:54:39

标签: 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('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在某些情况下没有等待足够长的时间。

我在test_helper中设置了Capybara.default_max_wait_time = 5

我已经尝试过硒和吵闹的司机,但没有区别。

这是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

有什么我可能遗失的吗?

0 个答案:

没有答案