[英]Cucumber Capybara Selenium race condition with expect(all())
我正在使用Rails 4应用程序。 在应用程序中,有一个具有模式形式的页面,我尝试对其行为进行如下测试:
<li>
表单 <li>
表单 <li>
元素 我的web_step用于计算<li>
元素的数量,使用的是Expect 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在JS在DOM中更改它们之前正在寻找<li>
元素进行计数(并且失败)。 我认为expect(all())
是罪魁祸首,但不确定重构或使其始终如一地工作的最佳方法。
修复失败
我试图通过几种方法来解决这种竞争状况,包括(1)增加Capybara.default_wait_time = 120
;(2)将sleep(10)
添加到web_steps(我认为是一种反模式);(3)在其中包装web_step功能page.document.synchronize
,如“ 如何在 page.document.synchronize
中完全使用同步”中所示。
我知道wait_until
方法已从水豚中删除,如下所示。
堆栈:铁轨4.0.3,黄瓜1.5.15,水豚2.2.1,硒webdriver 2.41.0,angular.js,jquery,引导程序。 FWIW…我的机器是Retina MacBook Pro i7,非常快。
任何想法表示赞赏。 谢谢!
Capybara提供选项(:count,:minimum,:maxium,:between)来指定要等待多少个元素。 假设您使用的是最新版本的Capybara,则现在可以在all
版本中使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.