javascript - 为什么Capybara/Rspec在Sweet Alert Modal中找不到按钮,但have_selector返回true?

标签 javascript ruby-on-rails rspec capybara poltergeist

这是 Rspec 功能。

given (:send_selector){ 'button.confirm'}

feature "when talent has not applied." do
  given (:apply_button) { find('button[data-selector="offer-apply-button"]') }
  before(:each)         { visit offer_path(offer.id)                         }

  scenario "Talent applies to offer with default cv", js: true do
    apply_button.click
    expect(page).to have_selector(send_selector)
    find_button(send_selector).click
    wait_for_ajax
    # other expectations
  end
end

它满足了选择器的期望,但随后抛出了错误

 Capybara::ElementNotFound:
       Unable to find button "button.confirm"

指向带有find_button(send_selector).click的行

我还尝试使用page.find_button(send_selector).clickfind_button(send_selector).trigger('click') 但第一个仍然抛出错误,第二个似乎什么也没做,因为负责操作的 Controller 从未被调用,并且模态仍然存在我在下一行截图。

我运行了 poltergeist 调试器,并且 html 出现在页面上。

html code from the poltergeist debugger

这是 capybara 看到的模式(使用 save_screenshot() 方法)

enter image description here

最佳答案

问题在于 find_button 不采用 CSS 选择器 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_button-instance_method - 它采用按钮的名称、id、标题或文本内容(与 click_button 和 'have_button' 相同)。

在您的回答中,sleep 1与解决问题无关,它已被修复,因为您从find_button交换到find,确实需要 CSS 选择器

关于javascript - 为什么Capybara/Rspec在Sweet Alert Modal中找不到按钮,但have_selector返回true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43213587/

相关文章:

ruby-on-rails - 如何删除单元测试并将其替换为 rspec?

javascript - 为什么单击 div 时 document.execCommand 不起作用?

javascript - 在 Rails 服务器上设置跨域调用

javascript - 从外部 JSON 文件生成选择列表

ruby-on-rails - 使用 HAML 在段落内应用类

html - Rails 图片 link_to 不工作

javascript - EJS的render()和renderFile()有什么区别?

ruby-on-rails - Rails + Devise HTTP header token

ruby-on-rails - RSpec,期望随着多个值的变化而变化

ruby-on-rails - 预期 true 会回应 true 吗?