-->

故障通过水豚与引导模态交互(V2)(Trouble interacting with Bootstr

2019-08-08 01:55发布

在Rails应用程序我想测试一个引导模式与jQuery的TokenInput场使用水豚用在Rspec的capybara-webkit驱动程序。 有问题的部分如下:

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按键得到它的选择
  • 创建团队
  • 验证名称的页面上显示出来

这只有在所有这些工作, sleep 1 S IN的地方; 否则水豚在崩溃have_content ,最终导致服务器错误,因为球队的名字从来没有能够适当地选择。 其他引导情态动词没有 TokenInput场不需要sleep 1加载完毕之前,但是。

说了这么多,有没有什么办法来摆脱睡的和有这种正常进行? 水豚2掏出wait_until (有很好的理由),因为它会默认等待时间内等待测试的东西...但是,这似乎并没有在我上面的测试中得到体现; 就好像水豚并不在等待周期在进入/退出这个模式搞。 有人对此有经验吗? 使用Rails 3.2.10,Rspec的2.12,水豚2,水豚-webkit的0.14.0,TokenInput 1.6。

Answer 1:

尝试禁用测试ENV动画,布局/ application.html.erb

<% if Rails.env.test? %>
 <style type="text/css">
    .modal.fade, .fade {
      -webkit-transition: opacity 0.01s;
      -moz-transition: opacity 0.01s;
      -ms-transition: opacity 0.01s;
      -o-transition: opacity 0.01s;
      transition: opacity 0.01s;
    }
 </style>
<%end%>


Answer 2:

我建议加上falowing测试ENV 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");

这解决了我的大部分问题,水豚和引导。



Answer 3:

我们只是做到这一点,似乎工作(例如点击$('.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()"


Answer 4:

对于希望避免这些Rails.env.___? 黑客*,下面似乎工作(到目前为止-手指交叉)在避免与测试问题jQuery UI的拖动和拖放功能,在基于自举模式。

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

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? 预选赛; 它会发生其他故障的测试套件的一半。



文章来源: Trouble interacting with Bootstrap modals via Capybara (v2)