简体   繁体   English

拖放在 Nightwatch + Selenium 中不起作用

[英]Drag & Dropping doesn't work in Nightwatch + Selenium

Using nightwatch and selenium, during a system test, I am trying to drag and drop, which is done with Knockout-draggable.使用 nightwatch 和 selenium,在系统测试期间,我尝试拖放,这是通过 Knockout-draggable 完成的。 It works 100% while using it manually.它在手动使用时 100% 工作。 This is the code from the system test which is supposed to drag and drop a draggable box:这是来自系统测试的代码,它应该拖放一个可拖动的框:

this.moveToElement('@box', 0, 0);
c.mouseButtonDown(0);
this.moveToElement('@box2', 0, 40);
c.mouseButtonUp(0);

this being the page (in which the xpath elements are) and c being the client. this是页面(xpath 元素所在的页面), c是客户端。

But this doesn't seem to even be able to move the box under the second box (which is about 40 pixels high).但这似乎甚至无法移动第二个框(大约 40 像素高)下的框。 Yes, I've tried different numbers and it doesn't even drag the box anywhere.是的,我尝试了不同的数字,它甚至没有将框拖到任何地方。 Done in Firefox.在 Firefox 中完成。

And yes, both @box and @box2 are working xpaths.是的, @box2 @box@box2都在使用 xpath。 I have been using those for a while for a bunch of other tests.我已经使用这些进行了一段时间的其他测试。

How about put a pause between c.mouseButtonDown(0);c.mouseButtonDown(0);之间暂停一下怎么样c.mouseButtonDown(0); and this.moveToElement('@box2', 0, 40);this.moveToElement('@box2', 0, 40);

Guys you have to try this and it works fine in Chrome, Firefox and IE.伙计们,你必须尝试这个,它在 Chrome、Firefox 和 IE 中运行良好。 But this works only if the element to move and the element to move to are both supporting HTML5 drag and drop feature.但这只有在要移动的元素和要移动到的元素都支持 HTML5 拖放功能时才有效。

Just you have to install "html-dnd" using npm, as well as this is a link -只是你必须使用 npm 安装“html-dnd”,这是一个链接——
https://www.npmjs.com/package/html-dnd https://www.npmjs.com/package/html-dnd

After installing you just have to execute this command:安装后,您只需执行以下命令:
browser.execute(dragAndDrop, ['#draggable', '#droppable']);

For example:例如:
browser.execute(dragAndDrop,['#elemendId1', '#elemendId2']).pause(2000);

Hope this will work fine for your test cases.希望这适用于您的测试用例。

I adapted @Hikaryu's method and it's working for me:我改编了@Hikyu 的方法,它对我有用:

exports.dragAndDrop = function (browser, LocatorFrom, LocatorTo) {
  browser
    .moveToElement(LocatorFrom,  10,  10)
    .mouseButtonDown(0)
    .pause(2000)
    .moveToElement(LocatorTo,  10,  10)
    .pause(2000)
    .mouseButtonUp(0);
  return browser;
};

I made a customCommand for this issue.我为这个问题做了一个 customCommand 。 It's working without a problem它工作没有问题

module.exports.command = function (LocatorFrom, LocatorTo, callback) {

  var self = this;

  this
    .getLocation(LocatorTo, function (result) {
      let xto = result.value.x;
      let yto = result.value.y;

      this.moveToElement(LocatorFrom,  10,  10)
        .mouseButtonDown(0)
        .pause(2000)
        .moveToElement(LocatorTo,  xto,  yto)
        .pause(2000)
        .mouseButtonUp(0);
      return this;
    });



  return this;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM