I've tried the solution provided here: How to implement drag and drop in cypress test? and have made more progress than when trying to use the cypress drag drop plugin, but have still not managed to get it to work properly.
Here is my code:
static dragMemberToDragDropList(memberName: string, dragDropListIndex: number): void {
cy.get('[autotestid="draws_draggable_member"]').contains(memberName).then(member => {
const draggable = member[0];
cy.get('[autotestid="draws_dragdrop_list"]').eq(dragDropListIndex).then(list => {
const coordinates = list[0].getBoundingClientRect();
draggable.dispatchEvent(new MouseEvent('mousedown'));
draggable.dispatchEvent(new MouseEvent('mousemove', {clientX: 10, clientY: 0}));
draggable.dispatchEvent(new MouseEvent('mousemove', {clientX: coordinates.left+10, clientY: coordinates.top+10}));
draggable.dispatchEvent(new MouseEvent('mouseup'));
cy.wait(500);
});
});
}
The problem I've got is that even though the coordinates seem to be correct the element is dragged to the wrong place. In the image below A is where it's being dragged from, C is where I want it to go and B is where it ends up...
I'm not sure if this is significant, but when I comment out the "mouseup" event so that when the test has completed I can see where the element ends up, and if I then move the mouse across the screen the element moves across the screen too. The bit that I think might be significant is that the element is some distance away from the mouse and when moving that exact distance and position between the two is maintained. The image below shows the relative positions (M is the mouse pointer):
I would have thought that the element would be in the same position as the pointer?
This issue is slowly driving me insane so any advice or suggestions would be appreciated!
P.S. I have also tried this:
it('drag drop test', () => {
cy.visit('https://material.angular.io/cdk/drag-drop/examples#cdk-drag-drop-enter-predicate');
cy.get('cdk-drag-drop-enter-predicate-example')
.find('.example-box').contains('2').then(number => {
const draggable = number[0];
cy.get('cdk-drag-drop-enter-predicate-example')
.find('#even').then(list => {
const coordinates = list[0].getBoundingClientRect();
draggable.dispatchEvent(new MouseEvent('mousedown'));
draggable.dispatchEvent(new MouseEvent('mousemove', {clientX: 10, clientY: 0}));
draggable.dispatchEvent(new MouseEvent('mousemove', {clientX: coordinates.left + 10, clientY: coordinates.top + 10}));
draggable.dispatchEvent(new MouseEvent('mouseup'));
cy.wait(500);
});
});
});
...but can't get that to work either.