簡體   English   中英

如何使用Rails RSpec和Capybara和Selenium進行測試以確保顯示模態

[英]Using Rails RSpec and Capybara and Selenium how can I test to make sure a modal is being displayed

我有一個必要的模式,用於在用戶尚未排除網站服務條款時顯示。 瀏覽大量文檔后,我需要進行測試以確保顯示引導程序模式,但我仍然對如何執行此操作感到迷惑。

# application.html.haml
.container-fluid
  - if user_has_not_accepted_terms?
    = render 'parts/terms_modal'

我的情態

.modal#terms_modal{'tabindex': "-1", role: "dialog", 'data- 
    keyboard':false, 'data-backdrop': "static"}
  .modal-dialog{role: "document"}
    .modal-content
      .modal-header
        %h5.modal-title
          Our Terms of Service have changed
      .modal-body
        %p
          Please take a minute and review our updated BLAH
      .modal-footer
        = link_to 'Accept Terms of Service', accept_terms_user_profile_path(current_user.id, current_user), method: :patch, class: 'btn btn-primary mx-auto'

js確保在頁面加載時打開模式

$(document).on('turbolinks:load', ->
  $('#terms_modal').modal('show')
)

$(document).on('page:load', ->
  $('#terms_modal').modal('show')
)

我知道我要測試的用戶尚未接受條款,條件返回true,同樣,當我在測試中運行調用save_and_open_page時,我也得到了包含模態的html

到目前為止,我已經嘗試過像

# accepting_terms_spec.rb
...
scenario 'user can accept terms of service', js: true do
  ... #user sign in and visit '/'

  within('.modal#terms_modal) do
    click_on('Accept Terms of Service')
  end

  expect(page).to have_content('Success')
end

我從測試中得到返回,表明.modal是不可見的。

由於該模式未在瀏覽器中打開,因此Capybara正確未通過測試。

最有可能是您的一個JS文件中的錯誤導致了此問題。 測試和開發環境之間的最大區別是,在測試(和生產)環境中,您所有的JS資產都被串聯到一個文件中。 這允許一個錯誤,以防止其他文件中的JS被處理。 在開發環境中不會發生這種情況,因為每個文件都是單獨提供的。 檢查您的瀏覽器開發者控制台是否存在JS錯誤並進行修復,該錯誤可能會消失。

可能性較小的可能性是您的資產管道已掛起,並且不再將新的JS更改合並到以測試模式提供的文件中。 您應該能夠使用rails assets:precompile強制進行重新rails assets:precompile盡管這可能仍然無法根據將來導致掛起的原因來對資產進行將來的更改。 在那種情況下,使用rails assets:clobber通常會重置管道以使其正常工作(可能需要執行RAILS_ENV=test rails assets:clobber/precompile以確保根據您的確切設置清除/構建了正確的東西)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM