selenium - RSpec/Capybara/JS 测试单独通过,但一起运行时失败

标签 selenium rspec capybara capybara-webkit database-cleaner

我的问题似乎非常类似于:rspec test passes in isolation, but fails when run with other tests - 但接受的答案是我已经在做的事情:我正在使用 avdi 的数据库清理器设置(并且我已经尝试删除而不是像thoughtbot在其吊带示例应用程序中使用的截断): http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

因此此代码中的帐户记录:

let(:account) { create(:account) }

似乎正在被删除。我正在访问找不到页面,因为该帐户不存在。那么在不同的数据库清理策略之间来回切换似乎是导致问题的原因?

我可以直接运行 rspec --only-failures 并且所有刚刚失败的测试都会通过。它仅发生在功能/ capybara 规范中。

编辑:这是我的配置:https://gist.github.com/gregblass/b886f79b0d8e2e2015af

更新:以下是一些失败测试的示例:https://gist.github.com/gregblass/1b878d92a2b9dad355e0

更新2:它发生在我做了 js: true 测试之后。这些东西搞砸了我随后的其他非 js capybara 测试。

最佳答案

我能够使用 rspec --bisect 并确定罪魁祸首 - 它们是 JS capybara 测试。在我运行 JS 测试之后,每个人似乎都推荐的数据库清理器切换策略是删除我在之后的非 js capybara 测试中声明的帐户。

这是一种解决方法:

http://stefan.magnuson.co/articles/rails/robust-integration-testing-in-rails-4-with-rspec-capybara-and-selenium/

Many tutorials and StackOverflow answers out there have JS and non-JS tests mixed together, with the strategy being switched between transaction and truncation on each test.

In my experience this has resulted in database access race conditions that have caused otherwise well-written, independent tests to fail intermittently.

The solution I have settled on is to run all non-JS tests first (shuffled), and then all JS tests (also shuffled). This allows for discovery of tests that incorrectly expect certain state, or fail to clean up after themselves (by virtue of the random execution order), while not attempting to freely mix JS and non-JS tests. As these different classes of test have different purposes, I see no disadvantage in this approach, with the exception of it being somewhat non-standard.

另一种方法是将数据库清理器配置更改为以下内容(取自 @ryanbigg 的 multitenancy with Rails 书籍):

config.before(:suite) do
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean_with(:truncation)
end

config.around(:each) do |example|
  DatabaseCleaner.cleaning do
    example.run
  end
end

速度较慢,但​​我什至无法在我的 Macbook Pro 16GB/SSD 上真正看出有多大区别。

感谢@tomwalpole 的所有帮助,@barelyknown 的建议 rspec --bisect,感谢 @ryanbigg 的关于使用 capybara 测试 Multi-Tenancy 的好书以及良好的工作代码示例,感谢 Stefan Magnuson 的那篇文章(我可以'找不到 SO 句柄)。

关于selenium - RSpec/Capybara/JS 测试单独通过,但一起运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074585/

相关文章:

ruby - RSpec 数组 instance_of 参数匹配器

ruby-on-rails - 在启用事务夹具的 Rails 4 中测试 after_commit 钩子(Hook)

Python、Selenium Webdriver : Spans are not loaded for Firefox driver

jquery - phantomjs/selenium 和 jQuery 自动填充表单的方式有区别吗?

rspec - 使用工厂女孩创建 has_many 关系的特征

ruby-on-rails - capybara :测试当前页面创建对象的路径

ruby-on-rails-4 - 使用 Capybara + Poltergeist 等待 AJAX

ruby-on-rails - 我如何解决 "Missing host to link to! Please provide the :host parameter"? (返回率)

android - org.openqa.selenium.WebDriverException : No command or response codec has been defined. 无法继续

ruby - capybara rspec 脚本未在描述 block 中执行