英文:
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被变灰,就好像被拖拽,但没有被释放。
请问有人能指出我做错了什么吗?
英文:
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
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论