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

3

我正在使用:

  • rspec
  • capybara
  • selenium
  • jquery ui-sortable
  • ruby
  • rails

这段代码:

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之外,拖放和排序工作正常。什么样的spec可以在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测试通过了。一切似乎都指向了左键释放事件之后应该发生的失败。


你尝试过使用PhantomJS吗?(Poltergeist宝石) - stytown
刚刚完成了。运行正常。速度更快了。这次更改引入了一些其他的小问题,我认为可以解决。谢谢。 - Perry Horwich
2个回答

1

你的问题在于你在期望中使用了find(...).text。在元素的查找和提取其文本之前,drop操作还没有生效。你应该使用的是

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

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

0

我转而使用了poltergeist宝石,现在拖放测试可以正常工作。

顺便说一下,这个转换需要对代码进行一些小的重写,以便将点击事件发送到被不可见对象覆盖的按钮。

无头驱动程序也更快。我有几个实例需要确保js服务器请求完成后才评估特定的测试用例。这个问题有几个解决方案,如此处所示:为什么rspec不等待上一个命令完成?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接