如何在 Cypress 测试中使用 cypress-drag-drop 包将一个 div 拖放到另一个 div?

huangapple go评论55阅读模式
英文:

How to drag and drop one div onto another in Cypress test using cypress-drag-drop package?

问题

我正在尝试编写一个 Cypress 测试,将列A拖放到该网页上的列B - https://the-internet.herokuapp.com/drag_and_drop

我安装了@4tw/cypress-drag-drop包,并将以下内容添加到我的support/commands.js中:

require("@4tw/cypress-drag-drop");

以下是我的 Cypress 代码:

cy.get("#column-a").drag("#column-b", { force: true });

测试通过,但是在视觉上,列没有像在手动将列A拖放到列B上时那样表现出相同的行为。

相反,在 Cypress Explorer 中浏览器上出现了以下情况:

如您所见,列A被变灰,就好像被拖拽,但没有被释放。

如何在 Cypress 测试中使用 cypress-drag-drop 包将一个 div 拖放到另一个 div?

请问有人能指出我做错了什么吗?

英文:

I am trying to write a Cypress test that drags & drops column A onto column B on this webpage - https://the-internet.herokuapp.com/drag_and_drop

I installed the @4tw/cypress-drag-drop package & added the following to my support/commands.js:
require("@4tw/cypress-drag-drop");

Here is my Cypress code:

cy.get("#column-a").drag("#column-b", { force: true });

The test passes, but the columns aren't behaving the same way visually as it does when I manually drag column A onto column B.

Instead, this is what appears on the browser in Cypress Explorer:

As you can see, column A is greyed out, as if it were dragged, but not dropped

如何在 Cypress 测试中使用 cypress-drag-drop 包将一个 div 拖放到另一个 div?

Can someone please point out what I'm doing incorrectly?

答案1

得分: 3

无法翻译代码部分。以下是翻译好的文本:

"You cannot, the library does not support the correct events for this page.

But you can do it using Cypress commands.

These are the events used on the page:

  • "dragstart" 事件监听器的处理函数为 handleDragStart。
  • "dragenter" 事件监听器的处理函数为 handleDragEnter。
  • "dragover" 事件监听器的处理函数为 handleDragOver。
  • "dragleave" 事件监听器的处理函数为 handleDragLeave。
  • "drop" 事件监听器的处理函数为 handleDrop。
  • "dragend" 事件监听器的处理函数为 handleDragEnd。

This is the test that passes:

// 检查初始顺序
cy.get('div.column')
.then($cols => [...$cols].map(col => col.innerText.trim()))
.should('deep.eq', ['A', 'B']);

const dataTransfer = new DataTransfer;

cy.get("#column-a")
.trigger('dragstart', { dataTransfer });
cy.get("#column-b")
.trigger('dragenter')
.trigger('dragover', { dataTransfer })
.trigger('drop', { dataTransfer });
cy.get("#column-a")
.trigger('dragend');

// 检查新顺序
cy.get('div.column')
.then($cols => [...$cols].map(col => col.innerText.trim()))
.should('deep.eq', ['B', 'A']);

// 检查拖动透明度是否恢复
cy.get("#column-a").should('have.css', 'opacity', '1');
cy.get("#column-b").should('have.css', 'opacity', '1');"

英文:

You cannot, the library does not support the correct events for this page.

But you can do it using Cypress commands.

These are the events used on the page

col.addEventListener("dragstart", handleDragStart, false);
col.addEventListener("dragenter", handleDragEnter, false);
col.addEventListener("dragover", handleDragOver, false);
col.addEventListener("dragleave", handleDragLeave, false);
col.addEventListener("drop", handleDrop, false);
col.addEventListener("dragend", handleDragEnd, false);

This is the test that passes

// check initial order
cy.get('div.column')
  .then($cols => [...$cols].map(col => col.innerText.trim()))
  .should('deep.eq', ['A', 'B'])

const dataTransfer = new DataTransfer;

cy.get("#column-a")
  .trigger('dragstart', {dataTransfer})
cy.get("#column-b")
  .trigger('dragenter')
  .trigger('dragover', {dataTransfer})
  .trigger('drop', {dataTransfer})
cy.get("#column-a")
  .trigger('dragend')

// check new order
cy.get('div.column')
  .then($cols => [...$cols].map(col => col.innerText.trim()))
  .should('deep.eq', ['B', 'A'])

// check drag opacity reverted back
cy.get("#column-a").should('have.css', 'opacity', '1')
cy.get("#column-b").should('have.css', 'opacity', '1')

huangapple
  • 本文由 发表于 2023年2月10日 02:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定