javascript – 在Cypress.io测试中没有发生拖放

我试图拖动一个元素,然后将其放入一个放置区域区域,但测试没有在Cypress.io中执行拖放操作.如果有人可以在这里就潜在问题提出建议,那将会非常有帮助.投掷没有错误,但仍然没有在这里发生拖放.

describe('Verify the drag and drop test', function() {

  it.only('Check whether the drag and drop of an item is working fine', function() {

  cy.visit('http://www.seleniumeasy.com/test/drag-and-drop-demo.html')  

    const MyDataTransfer = function () {};
    const dt = new MyDataTransfer ();
    dt.types = [];

    // cy.wait(3000);

    cy.get('#todrag span')
      .contains('Draggable 1')
      .trigger("draggable", {dataTransfer: dt});

    cy.get("#todrag span")
      .contains('Draggable 1')
      .trigger('mousedown', { which: 1, pageX: 600, pageY: 600 })
      .trigger('mousemove', { which: 1, clientX: 330, clientY: 35 });

    cy.wait(3000);

    cy.get('#mydropzone')
      .trigger("dropzone", {dataTransfer: dt});                     
  });   
});

《javascript – 在Cypress.io测试中没有发生拖放》

最佳答案 一些改变应该达到你想要的.以下是解决此问题所采取步骤的细分……

首先,不是简化MyDataTransfer,而是构造一个新的DataTransfer对象,它包含了拖动事件所需的必要属性和方法:

const dataTransfer = new DataTransfer;

接下来,最好触发native drop and drag events,而不是draggable和dropzone. Selenium Easy正在收听dragstart,dragend和drop(你可以在他们的drag-and-drop-demo.js source file中看到这个).将这些放在辅助函数中,稍后在测试中调用:

function dndIt() {
  cy.get('#todrag span:first-of-type')
    .trigger('dragstart', { dataTransfer });

  cy.get('#mydropzone')
    .trigger('drop', { dataTransfer });

  cy.get('#todrag span:first-of-type')
    .trigger('dragend');               // <-- seleniumeasy listens for this...
}                                      // otherwise, draggables will copied.

beforeEach块有助于设置视口和访问应用程序:

beforeEach(function() {
  cy.viewport(1000, 600);
  cy.visit('https://www.seleniumeasy.com/test/drag-and-drop-demo.html');
});

最后,测试块调用辅助函数,并断言该项被拖放:

it('Check whether the drag and drop of an item is working fine', function() {
  dndIt();

  cy.get('#droppedlist')
    .should(($el) => {
      expect($el.children()).to.have.lengthOf(1)
    });
});

以下是完整的解决方案:

describe('Verify the drag and drop test', function() {
  const dataTransfer = new DataTransfer;

  function dndIt() {
    cy.get('#todrag span:first-of-type')
      .trigger('dragstart', { dataTransfer });

    cy.get('#mydropzone')
      .trigger('drop', { dataTransfer });

    cy.get('#todrag span:first-of-type')
      .trigger('dragend');               // <-- seleniumeasy listens for this...
  }                                      // if left out, draggables are copied.

  beforeEach(function() {
    cy.viewport(1000, 600);
    cy.visit('https://www.seleniumeasy.com/test/drag-and-drop-demo.html');
  });

  it('Check whether the drag and drop of an item is working fine', function() {
    dndIt();

    cy.get('#droppedlist')
      .should(($el) => {
        expect($el.children()).to.have.lengthOf(1)
      });
  });
});

《javascript – 在Cypress.io测试中没有发生拖放》

cypress-example-recipes repo中提供的drag_n_drop_spec.js是一个有用的资源.

点赞