Javascript 为什么水豚/Rspec可以';在Sweet Alert模式中找不到按钮,但选择器是否返回true?
这是一个Rspec特性Javascript 为什么水豚/Rspec可以';在Sweet Alert模式中找不到按钮,但选择器是否返回true?,javascript,ruby-on-rails,rspec,capybara,poltergeist,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)
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_按钮(send_selector)。trigger('click')
但是第一个仍然抛出错误,而第二个似乎什么也没做,因为负责操作的控制器从未被调用,并且当我在下一行截图时,模式仍然存在
我运行poltergeist调试器,页面上显示html
这是水豚看到的模式(使用save\u screenshot()
方法)
如果有人有同样的问题,在模式出现后再加上一点睡眠就可以了。这种行为仍然很奇怪,因为期望值表示选择器已经存在。即使有\u选择器(发送\u选择器,可见:true)。显示?
返回true
scenario "Talent applies to offer with default cv", js: true do
apply_button.click
expect(page).to have_selector(send_selector)
sleep 1 <<-- THIS SOLVED THE PROBLEM
find(send_selector).click
wait_for_ajax
...
end
场景“人才申请提供默认简历”,js:true-do
应用按钮。单击
期望(第页)。有\u选择器(发送\u选择器)
睡眠1问题在于find_按钮
不采用CSS选择器——它采用按钮的名称、id、标题或文本内容(与click_按钮
和“have_按钮”相同)
在您的回答中,sleep 1
与解决问题无关,它是固定的,因为您从find_按钮
切换到了find
,该按钮使用CSS选择器我在使用sweetalert2时也遇到了这个问题,我在功能规范中解决了它,如下所示。(成功了)
嗨,托马斯,你说得对,find_按钮没有css选择器。尽管如此,我这次在没有sleep 1
的情况下运行了我在答案中发布的测试,但失败了。@juliangzalez如果是相同的错误,那么可能意味着您需要将Capybara.default\u max\u wait\u时间增加几秒钟。然而,如果这是一个不同的错误(关于鼠标点击的某些东西会击中不同的元素),那么这是因为模态仍在动画中,您将需要睡眠或更好地在测试模式下禁用动画,这将加快您的测试并防止一些潜在的片状
scenario 'destroy a company', js: true do
within('.shadow.company-info') do
find('a[data-behavior="delete"]').click
wait_for_ajax
within(page.document.find(:css, '.swal2-buttonswrapper')) do
find(:css, '.swal2-confirm').click
wait_for_ajax
expect(page.document).to have_content I18n.t('delete.success')
end
end
end