简体   繁体   English

如何在cypress中拖放ul> li元素

[英]How to drag and drop ul > li elements in cypress

In the system, there are two areas to be considered in automating drag and drop functionality. 在系统中,自动化拖放功能需要考虑两个方面。 One thing which called "timesheet" is in the div and another one is in ul > li . div有一个叫做“时间表”的东西,而ul > li是另一种东西。 Here is the timesheet html sample, 这是时间表html示例,

<div class="time-update__top-block">

In the test scripts, I used this way to drag and drop as follows, 在测试脚本中,我使用了这种方式,如下所示进行拖放,

cy.get(".time-update-block", { timeout: 60000 })
      .trigger("mousedown", { force: true })
      .wait(2000);
    cy.get(
      '.anotherElement',
      { timeout: 60000 }
    )
      .trigger("mousemove", { force: true }, "topLeft")
      .wait(2000);
    cy.get(
      '.anotherElement',
      { timeout: 60000 }
    )
      .trigger("mouseup", { force: true }, "topLeft")
      .wait(2000);

Above code was executed successfully and the element was dragged and dropped in the way I wanted. 上面的代码已成功执行,并且按照我想要的方式拖放了元素。

Other one's html sample as follows, 他人的html示例如下,

 <ul class="schedule-movie-list">
    <li class="undefined schedule-movie-list__item " title="Drag me!" draggable="true">Name1</li>
    <li class="undefined schedule-movie-list__item " title="Drag me!" draggable="true">Name2</li>
    <li class="undefined schedule-movie-list__item " title="Drag me!" draggable="true">Name3</li></ul>

In the test scripts, I used this way to drag and drop as follows, 在测试脚本中,我使用了这种方式,如下所示进行拖放,

cy.get(".schedule-movie-list > .schedule-movie-list__item", {
      timeout: 60000
    })
      .eq(1)
      .click()
      .trigger("mousedown", { force: true });

    cy.get(
      '.anotherElement2',
      { timeout: 60000 }
    ).trigger("mousemove", { force: true }, "topLeft");
    cy.get(
      '.anotherElement2',
      { timeout: 60000 }
    ).trigger("mouseup", { force: true }, "topLeft");

But eventually above code was not executed successfully. 但是最终上述代码没有成功执行。 The element was not dragged and dropped automatically. 元素未自动拖放。 If anyone has an idea to resolve this highly appreciated. 如果有人有解决此问题的想法,将不胜感激。

Using dataTransfer const, we can get what we need to achieve; 使用dataTransfer const,我们可以获得需要实现的目标;

describe("Main Page", () => {
  const dataTransfer = new DataTransfer;
  it("Drag & Drop elements", () => {
      cy.get('.schedule-movie-list > .schedule-movie-list__item')
        .first()
        .trigger('dragstart', { dataTransfer });

      cy.get(schedulerPerformancePanel)
        .first()
        .trigger('drop', { dataTransfer })
        .wait(3000);

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

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