通过Capybara(v2)与Bootstrap模态进行交互时遇到问题

在Rails应用程序中,我正在尝试使用Capybara和capybara capybara-webkit驱动程序在Rspec中使用jQuery TokenInput字段测试Bootstrap模式 。 有问题的部分如下:

 click_link 'Create Team Modal' sleep 1 within('div#modal_popup') do fill_in 'input#token-input-team_name', with: 'Fancy team name' sleep 1 fill_in 'input#token-input-team_name', with: '\t' sleep 1 click_button 'Create Team' end page.should have_content('Fancy team name') 
  • 单击按钮以获取模态
  • 使用团队名称填写TokenInput
  • 模拟Tab键 – 按下以选中它
  • 创建团队
  • validation页面上显示的名称

这只适用于所有那些sleep 1的人; 否则have_contenthave_content崩溃,最终导致服务器错误,因为团队名称永远无法正确选择。 但是, 没有 TokenInput字段的其他Bootstrap模式在加载之前不需要sleep 1

尽管如此,有没有办法摆脱睡眠并让这种情况正常进行? wait_until 2拿出了wait_until (有充分的理由),因为它会在默认的等待时间内等待测试…但是这似乎没有反映在我的上述测试中; 就好像Capybara在进入/退出这个模态时没有参与等待期。 有人对此有经验吗? 使用Rails 3.2.10,Rspec 2.12,Capybara 2,capybara-webkit 0.14.0,TokenInput 1.6。

尝试在test env,layouts / applicaiton.html.erb中禁用动画

 <% if Rails.env.test? %>  <%end%> 

我建议在测试环境中添加falowing css:

  div, a, span, footer, header { -webkit-transition: none !important; -moz-transition: none !important; -ms-transition: none !important; -o-transition: none !important; transition: none !important; } .modal { display: none !important; } .modal.in { display: block !important; } .modal-backdrop { display: none !important; } 

在身体和身体中添加这个js:

 $(".fade").removeClass("fade"); 

这解决了我在水豚和引导程序中遇到的大部分问题。

我们只是这样做它似乎工作(例如点击$('.tp-header-login' ):

 # instead of find(".tp-header-login") find(".tp-header-login") # still do the find so you are sure its loaded then... execute_script "$('.tp-header-login').click()" 

对于那些希望避免Rails.env.___? 黑客*,以下似乎工作(到目前为止 – 手指交叉)避免在基于Bootstrap的模式上测试jQuery UI拖放function的问题。

首先,我们已经“等待”出现模式,使用这样的辅助方法:

 def wait_for_modal_to_appear modal = wait_until { # Look for the modal by ID/whatever... } raise Capybara::ModalNotFound.new('...') if modal.nil? return modal end 

然而,当我试图在该模态中拖放元素时,我们遇到了虚假的问题。 在return行之前添加的以下代码添加似乎已经完成了诀窍:

 page.execute_script("document.getElementById('#{modal[:id]}').classList.remove('fade');") 

*最近这样的黑客行为导致需要在我合作的公司进行紧急部署……一个糟糕的代码更改成功投入生产,因为它只是由一个if Rails.env.production?激活if Rails.env.production? 预选赛; 否则它会失败一半的测试套件。

Interesting Posts