使用Leadfoot测试拖放

时间:2014-07-31 08:54:39

标签: testing selenium drag-and-drop intern

我们正在使用Intern和优秀的新Leadfoot客户端库测试一个简单的拖放行为。我们的要求很简单:

  1. 找一个元素并移动到它
  2. 按下鼠标按钮(拿起可拖动的项目)
  3. 找到另一个元素并移动到它(拖动)
  4. 释放鼠标按钮(放下可拖动项目)
  5. 检查发生的事情
  6. 我希望以下示例语法能够执行此任务:

    .findById("MY_DRAGGABLE")
      .moveMouseTo()
      .pressMouseButton()
    .end()
    
    .findById("MY_DROP_ZONE")
      .moveMouseTo()
      .releaseMouseButton()
    .end()
    

    这不起作用。我已经知道Selenium可能存在一些特质,并且有一些'移动'可能需要拿起可拖动的东西:

    .findById("MY_DRAGGABLE")
      .moveMouseTo()
      .pressMouseButton()
      .moveMouseTo(null, 1, 1)
    .end()
    

    这仍然不起作用,实际上是为了使这个“拾音器”成为现实。我必须添加一个click():

    .findById("MY_DRAGGABLE")
      .moveMouseTo()
      .click()
      .pressMouseButton()
      .moveMouseTo(null, 1, 1)
    .end()
    

    此部分现在可以使用并且可以拖动,但移动和释放不起作用。事实上,为了让他们做我想做的事情,我必须使用一个相当奇怪的语法来突破承诺链:

    .findById("MY_DROP_ZONE")
      .then(function(element) {
        browser.moveMouseTo(element)
      })
      .releaseMouseButton()
    .end()
    

    所以最后我有:

    .findById("MY_DRAGGABLE")
      .moveMouseTo()
      .click()
      .pressMouseButton()
      .moveMouseTo(null, 1, 1)
    .end()
    
    .findById("MY_DROP_ZONE")
      .then(function(element) {
        browser.moveMouseTo(element)
      })
      .releaseMouseButton()
    .end()
    

    我有兴趣知道的是,我在这里遗失的对象定位是否存在,或者我们使用的命令是否存在错误?

    我们主要在各种平台上对Firefox进行测试。

1 个答案:

答案 0 :(得分:1)

不幸的是,Selenium doesn't support HTML5 drag-and-drop,所以当浏览器注册鼠标按下并释放时,拖动操作不会。

一种解决方案是使用JavaScript来模拟拖放过程。如何执行此操作的示例是Leadfoot的dnd分支中的DragAndDrop helper