测试拖放不起作用 rspec/capybara/selenium/jquery

Testing drag and drop not working rspec/capybara/selenium/jquery

我正在使用:

此代码:

expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "0")
expect(find_by_id("note1").text).to eq(note_placeholder.to_s + "1")
expect(find_by_id("note2").text).to eq(note_placeholder.to_s + "2")
source=find_by_id("combo0")
target=find_by_id("combo1")
source.drag_to(target)    
expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "1")
expect(find_by_id("note1").text).to eq(note_placeholder.to_s + "0")
expect(find_by_id("note2").text).to eq(note_placeholder.to_s + "2")

... 不会抛出错误,但是

expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "1")

...失败

 Failure/Error: expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "1")

   expected: "imaging_step_note1"
        got: "imaging_step_note0"

似乎drag_to 不工作。有没有一种好方法可以确认这确实是问题所在?有好的解决方法吗?

拖放和后续排序在 rspec 之外工作正常。在 rspec 中,什么样的规范会对此进行测试?

编辑: 这似乎提供了一些承诺,但我不确定我是否理解得很好:
https://github.com/mattheworiordan/jquery.simulate.drag-sortable.js/blob/master/README.md

这是一个好的选择吗?似乎必须有更好的方法,不是吗?这不是要测试的通用功能吗?我错过了什么?

编辑 2:
2011 年的另一个选项:
https://ynot408.wordpress.com/2011/09/22/drag-and-drop-using-selenium-webdriver/ ...虽然 javascript 的单条长线目前超出了我的范围。

编辑3: 基于此:
http://www.seleniumhq.org/docs/03_webdriver.jsp#setting-up-webdriver-project

...我试过这段代码:

source = page.driver.browser.find_element(:id, 'combo0')
target = page.driver.browser.find_element(:id, 'combo1')
page.driver.browser.action.drag_and_drop(source, target).perform
sleep 5

...但我仍然得到:

Failure/Error: expect(find_by_id("note0").text).to eq(note_placeholder.to_s + "1")

   expected: "imaging_step_note1"
        got: "imaging_step_note0"

编辑4:
刚刚从 2.42.0 更新到 selenium-webdriver (2.45.0) 结果相同。现在唯一的区别是规范似乎 运行 慢得多。恢复到 2.42.0.

编辑5: 如果我添加:

sleep 15

...并手动执行拖放,rspec 测试通过。一切似乎都指向在鼠标左键释放事件之后应该发生的任何事情都失败了。

我转而使用 poltergeist gem,现在可以进行拖放测试了。

FWIW,该开关需要重写一些次要代码才能将点击事件发送到被不可见对象重叠的按钮。

无头驱动程序也快得多。我有几个实例,在评估特定测试用例之前,我必须确保 js 服务器请求已完成。这个问题有几个解决方案,如下所示:Why rspec doesn't wait completion of previous command?

您的问题是您在期望中使用 find(...).text。元素的查找以及其文本的提取发生在放置生效之前。您想要使用的是

expect(page).to have_css('#note0', text: note_placeholder.to_s + "1")

这将导致 Capybara 等待元素 #note0 的最长默认等待时间,其中指定的文本内容出现在页面上