如何拖放Cypress.io

时间:2019-05-23 20:20:25

标签: javascript testing drag-and-drop e2e-testing cypress

我正在测试Trello,并尝试拖动最后一个列表,然后将其放入倒数第二列,但是如果没有“ .wait”,测试将无法进行。如果有人可以就此处的潜在问题提出建议,那将非常有帮助,因为我更喜欢避免使用“ .wait”。没有抛出任何错误,但是如果没有“ .wait”,拖放操作就不会发生。

describe("Moving list", () => {
  it("Waiting For Accept list should be moved from last column to the penultimate column", () => {
    cy.get("#board")
      .children(".js-list")
      .should("have.length", 4)
      .and("be.visible");

    cy.get(":nth-child(4) > .list")
      .should("be.visible")
      .and("contain", "Waiting For Accept")

    cy.get(":nth-child(4) > .list").trigger("mousedown", {
      which: 1
    });

    cy.get("#board > div:nth-child(2) > .list")
      .trigger("mousemove");

    cy.get("#board > div:nth-child(3) > .list")
      .trigger("mousemove")
      .trigger("mouseup");

    cy.get(":nth-child(3) > .list")
      .should("contain", "Waiting For Accept");
  });
});

See image

See image

4 个答案:

答案 0 :(得分:0)

开箱即用,该问题的记录为https://github.com/cypress-io/cypress/issues/845。但是在同一票证中,也可以使用可拖放元素上具有可拖动属性的本地拖放API来解决此问题:

创建自定义命令

Cypress.Commands.add("dragTo", { prevSubject: "element" }, (subject, targetEl) => {
    cy.wrap(subject).trigger("dragstart");
    cy.get(targetEl).trigger("drop");
  }
);

在测试脚本中,您可以使用:

cy.get(".source").dragTo(".target");

答案 1 :(得分:0)

最后,我使用“ cy.request”解决了这个问题

https://docs.cypress.io/api/commands/request.html#Syntax

describe("Moving list", () => {
        it("Waiting For Accept list should be moved from last column to the penultimate column", () => {
            cy.request("https://trello.com/b/9lfzKIRu/trello-tests").then(response => {
                expect(response.status).to.eq(200);
            });
            cy.get("#board > div:nth-child(4) > .list").trigger("mousedown", {
                which: 1
            });
            cy.get("#board > div:nth-child(2) > .list").trigger("mousemove");
            cy.get("#board > div:nth-child(3) > .list")
                .trigger("mousemove")
                .trigger("mouseup");
            cy.get(":nth-child(3) > .list").should("contain", "Waiting For Accept");
        });
    });

答案 2 :(得分:0)

您可以使用赛普拉斯drap拖放插件

https://github.com/4teamwork/cypress-drag-drop

答案 3 :(得分:0)

最好的解决方案是使用 https://github.com/4teamwork/cypress-drag-drop 它也支持拖动到顶部和底部。

        cy.get('#placeholder-3').drag('#placeholder-2', {            position: 'top', })