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.

Drag and drop with jquery ui sortable

See original GitHub issue
  • Operating System: Mac os
  • Cypress Version: last version
  • Browser Version: Chrome and Electron

Hello,

I’m trying to use Cypress to test a drag and drop sort (jquery ui sortable). It seems not working here. Is there an example? I tried this without success :

    cy.get(".grip").first().trigger('mouseover')
      .trigger('mousedown',  { which: 1 })
      .trigger('mousemove', {clientX: 600, clientY: 600})
      .trigger('mouseup', {force: true});

.grip is the handle. The parent (tr) is the draggable element.

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
pabloleonecommented, Feb 15, 2022

This worked for me:

  cy.get("#element")
    .then((el) => {
    const rect = el[0].getBoundingClientRect();
    const pageYDragAmount = 100;

    cy.window().then((window) => {
        const pageY = rect.top + window.pageYOffset;

        cy.wrap(el)
        .trigger("mousedown", {
            which: 1,
            pageX: rect.left,
            pageY,
            force: true,
        })
        .trigger("mousemove", {
            pageX: rect.left,
            pageY: pageY + pageYDragAmount,
            force: true,
        })
        .trigger("mousemove") // -- notice this new call
        .trigger("mouseup", {
            which: 1,
            force: true,
        });
    });
});
4reactions
chrisbreidingcommented, Apr 12, 2018

I experimented with this a bit and I think I’ve figured out what jQuery UI needs to simulate drag-n-drop. It requires pageX and pageY instead of the client* equivalents. It requires them for the mousedown as well as the mousemove and requires which: 1 for the mousemove. It should look like this:

cy.get(".grip").first()
  .trigger('mousedown', { which: 1, pageX: 600, pageY: 100 })
  .trigger('mousemove', { which: 1, pageX: 600, pageY: 600 })
  .trigger('mouseup');

You just need to make sure the pageX and pageY values actually line up with where your element is for mousedown and where you need it to be dropped for mousemove.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sortable | jQuery UI
Enable a group of DOM elements to be sortable. Click on and drag an element to a new spot within the list, and...
Read more >
Jquery UI combine sortable and draggable - Stack Overflow
Both drag'n drop and sortable functions shares the droppable:drop function. When sorting elements, the function has to replace the selected ...
Read more >
Drag and Drop Element with JQuery UI Sortable - Phpflow.com
Step 2: We will Define HTML Structure or HTML view file to show drag and drop elements list.HTML structure of jQuery UI Sortable...
Read more >
Drag and drop with jQuery UI Sortable - Simon Battersby
Using jQuery UI Sortable to change the sort order of a list of items via a drag and drop interface.
Read more >
Web Design: Drag and Drop with jQuery UI Sortable - Hongkiat
This particular plugin will enable a group of DOM to be sortable, meaning that we are able to move the object from one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

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

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