【问题标题】:Cucumber Capybara Selenium race condition with expect(all())带有期望(all())的黄瓜水豚硒竞争条件
【发布时间】:2014-08-10 14:13:52
【问题描述】:

我正在开发 Rails 4 应用程序。在应用程序中,有一个带有模态表单的页面,我正在尝试测试其行为如下:

  1. 点击链接打开模态,
  2. 点击链接通过JS添加<li>表单
  3. 输入一些字段数据,通过JS提交
  4. 查看填充的表单项是否存在
  5. 点击链接通过JS添加另一个<li>表单
  6. 再输入一些字段数据,通过JS提交
  7. 查看填充的表单项是否存在
  8. 确认<li>元素的正确数量

我用于计算 <li> 元素数量的 web_step 使用 expect(all()) RSpec 期望,如下所示:

Then(/^I should see the number of items equals "(.*?)"$/) do |number_of_ items |
    expect(all(:xpath, xpath_items).length).to eq(number_of_ items.to_i)
end

xpath_items 正在正确收集元素。

问题

我遇到间歇性黄瓜测试失败。我认为 Capybara 正在寻找 <li> 元素在 JS 在 DOM 中更改它们之前计数(并且失败)。我认为expect(all()) 是罪魁祸首,但不确定重构或使其始终如一地工作的最佳方法。

修复失败

我尝试通过几种方法来解决这种竞争条件,包括 (1) 增加 Capybara.default_wait_time = 120,(2) 将 sleep(10) 添加到 web_steps(我认为是一种反模式),(3) 将 web_step 功能包装在page.document.synchronize 如图 How to use synchronize in Capybara exactly?

我知道 wait_until 方法已从 Capybara 中删除,如图所示 here.

堆栈:rails 4.0.3、cucumber 1.5.15、capybara 2.2.1、selenium-webdriver 2.41.0、angular.js、jquery、bootstrap。 FWIW……我的机器是 Retina MacBook Pro i7,速度非常快。

任何想法表示赞赏。谢谢!

【问题讨论】:

    标签: javascript ruby-on-rails selenium cucumber capybara


    【解决方案1】:

    Capybara 提供了选项(:count, :minimum, :maxium, :between)来指定等待多少元素。假设您使用的是最新版本的 Capybara,这些现在可以在 all 中使用。

    通过指定:count,您可以告诉all 方法等待特定数量的项目出现。这可以添加到您的步骤中:

    Then(/^I should see the number of items equals "(.*?)"$/) do |number_of_ items|
      expect(all(:xpath, xpath_items, count: number_of_ items.to_i).length).to eq(number_of_ items.to_i)
    end
    

    但是,这对我来说似乎有点难以阅读。我建议改用have_selector,它确实支持这些选项。有了这个,您可以将步骤简化为:

    Then(/^I should see the number of items equals "(.*?)"$/) do |number_of_ items|
      expect(page).to have_selector(:xpath, xpath_items, count: number_of_ items.to_i)
    end
    

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多