question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

data-react-beautiful-dnd-draggable does not work with cypress drag and drop command

See original GitHub issue

Hi,

I have been trying to understand why the mouse/keyboard cypress commands don’t work with the ‘data-react-beautiful-dnd-draggable’ in react for a web application we have developed. I am running the cypress tests on Chrome on an element of type ‘form’.

I am attempting to drag and drop a series of text boxes by swapping them with each other. These text boxes can be identified using ‘form id=X’. The text boxes also have a ‘data-position’ attribute. On drag and drop these text boxes should fire an API call that gets the text boxes reordered but so far had no luck actually moving the textboxes around.

I have tried the following approaches:

  • dnd plugin
  • following the cypress dnd recipe
  • using keyboard actions such as keyDown/keyUp
  • using dragstart,dragend,mouseover,mousedown

This is a snippet of the code I was running: Keyboard actions:

describe("Reorder text boxes", () => {
    it("should reorder textboxes", function() {
      cy.wait(2000);
      cy.get("div[class*=index_banner]:nth-child(2)")
        .focus()
        .trigger("space", { keyCode: space, force: true })
        .trigger("keyup", { keyCode: arrowUp, force: true })
        .wait(5000)
        .trigger("space", { keyCode: space, force: true });
      });
  });

Mouse actions:

              cy.get(`form[id=309]`)
              .parent() // locates dnd-react component
              .focus()           
              .trigger('mousedown', { clientX: 338 , clientY: 268 })              
              .trigger('mousemove', { clientX: 338 , clientY: 262 })
              wait(3000)
              .trigger('mouseout', { clientX: 339 , clientY: 258 })
              .trigger('mouseleave', { clientX: 339 , clientY: 258 })
              .trigger('mouseover', { clientX: 318, clientY: 262})

HTML image

I have also tried to use the invoke() function to change the ‘data-position’ attribute.

Any help would be greatly appreciated!

Cypress package version: 3.1.5 Chrome 73

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

98reactions
Shelexcommented, Apr 23, 2019

@sherryleneg Or custom command based on coords and mousevents, accepts 2 arguments - draggable and target selectors:

Cypress.Commands.add('dragAndDrop', (subject, target) => {
    Cypress.log({
        name: 'DRAGNDROP',
        message: `Dragging element ${subject} to ${target}`,
        consoleProps: () => {
            return {
                subject: subject,
                target: target
            };
        }
    });
    const BUTTON_INDEX = 0;
    const SLOPPY_CLICK_THRESHOLD = 10;
    cy.get(target)
        .first()
        .then($target => {
            let coordsDrop = $target[0].getBoundingClientRect();
            cy.get(subject)
                .first()
                .then(subject => {
                    const coordsDrag = subject[0].getBoundingClientRect();
                    cy.wrap(subject)
                        .trigger('mousedown', {
                            button: BUTTON_INDEX,
                            clientX: coordsDrag.x,
                            clientY: coordsDrag.y,
                            force: true
                        })
                        .trigger('mousemove', {
                            button: BUTTON_INDEX,
                            clientX: coordsDrag.x + SLOPPY_CLICK_THRESHOLD,
                            clientY: coordsDrag.y,
                            force: true
                        });
                    cy.get('body')
                        .trigger('mousemove', {
                            button: BUTTON_INDEX,
                            clientX: coordsDrop.x,
                            clientY: coordsDrop.y,
                            force: true            
                        })
                        .trigger('mouseup');
                });
        });
});
8reactions
alexreardoncommented, Apr 23, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress - Drag and drop not working on a react-based website
I have the same issue and I found this solution. Hope it works for you: Add the following code to your cypress/support/commands.js file....
Read more >
How to test drag and drop functionality with Cypress and react ...
How to test drag and drop functionality with Cypress and react-dnd. First, to search for items easily, I am using the React Testing...
Read more >
@4tw/cypress-drag-drop - npm
A cypress child command for drag'n'drop support.. Latest version: 2.2.3, last published: 15 days ago. Start using @4tw/cypress-drag-drop in ...
Read more >
How to perform Drag and Drop on HTML and Angular sites ...
In this article, we will discuss in detail how we can perform drag and drop operations on HTML and Angular sites with cypress....
Read more >
react-beautiful-dnd: Versions - Openbase
The previous approach did not work well in a number of scenarios including combining and ... DraggableStyle, - 'data-react-beautiful-dnd-draggable': string, ...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found