Capybara慢速测试,视图渲染需要花费大量时间

时间:2015-08-07 09:59:22

标签: ruby-on-rails tdd capybara capybara-webkit poltergeist

问题仍存在于Rails 4.2和最新的水豚身上。

我认为这不是特定于驱动程序的问题,因为它使用rack_test,capybara-webkit甚至是poltgeist都会变慢。

渲染大约需要78086ms。

示例日志输出:

   (6.5ms)  COMMIT
Started GET "/coming-soon" for 127.0.0.1 at 2015-08-07 11:53:27 +0200
Processing by FeaturesController#index as HTML
  TranslationKey Load (10.0ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "features.page-description"]]
  Feature Load (0.5ms)  SELECT "features".* FROM "features"
  TranslationKey Load (0.5ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "features.see_preview"]]
  TranslationKey Load (0.4ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "features.form-description"]]
  Rendered features/index.html.haml within layouts/application (67.2ms)
  TranslationKey Load (0.4ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "meta.title"]]
  TranslationKey Load (0.4ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "meta.description"]]
  TranslationKey Load (0.3ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "meta.keywords"]]
  MenuItem Load (6.1ms)  SELECT "menu_items".* FROM "menu_items" WHERE "menu_items"."menu_category" = $1  ORDER BY position ASC  [["menu_category", "3"]]
  TranslationKey Load (0.2ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "page.instagram_url"]]
  TranslationKey Load (0.2ms)  SELECT  "translation_keys".* FROM "translation_keys" WHERE "translation_keys"."key" = $1 LIMIT 1  [["key", "page.pinterest_url"]]
  Rendered shared/_header.html.haml (416.5ms)
  MenuItem Load (0.2ms)  SELECT "menu_items".* FROM "menu_items" WHERE "menu_items"."menu_category" = $1  ORDER BY position ASC  [["menu_category", "1"]]
  Rendered shared/_flash_messages.html.haml (5.4ms)
  Rendered shared/_analytics.html.erb (0.5ms)
Completed 200 OK in 78086ms (Views: 78046.3ms | ActiveRecord: 21.5ms)

TransitionKeys是fast_gettext的东西。 MenuItems是一个单元格(单元格宝石)。

我使用以下宝石进行测试:

group :test do
  gem 'factory_girl_rails'
  gem 'mocha'
  gem 'shoulda'
  gem 'database_cleaner'
  gem "minitest-rails-capybara"
  gem 'capybara-email'
  gem 'capybara-webkit'
  gem 'poltergeist'
  gem 'cucumber-rails', :require => false
end

我也尝试过更改事务和截断策略。

怎么了?

修改

当我启动本地服务器时: RAILS_ENV =测试捆绑执行轨道c
即使config.assets.debug = false

,它也很慢

2 个答案:

答案 0 :(得分:2)

如果这只发生在第一次运行的测试中,那么这与Capybara无关,很可能是编译资产的资产管道。您可以在运行测试之前尝试在测试环境中对它们进行预编译,看看是否加快了测试速度。

答案 1 :(得分:0)

确保在相关的config.assets.debug = false文件中设置了config/environments/*.rb(可能只是test.rb)。