![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@4tw/cypress-drag-drop
Advanced tools
@4tw/cypress-drag-drop is a Cypress plugin that allows you to easily simulate drag-and-drop interactions in your end-to-end tests. This can be particularly useful for testing applications with complex UI interactions that involve dragging and dropping elements.
Basic Drag and Drop
This feature allows you to drag an element with the ID 'drag-element' and drop it onto an element with the ID 'drop-target'.
cy.get('#drag-element').drag('#drop-target');
Drag and Drop with Offset
This feature allows you to drag an element with the ID 'drag-element' and drop it onto an element with the ID 'drop-target' with specific offsets for the X and Y coordinates.
cy.get('#drag-element').drag('#drop-target', { offsetX: 10, offsetY: 20 });
Drag and Drop with Force
This feature allows you to force the drag-and-drop action even if the element is not visible or interactable.
cy.get('#drag-element').drag('#drop-target', { force: true });
cypress-real-events is a Cypress plugin that provides real user events like click, double-click, and drag-and-drop. It uses the native browser events to simulate user interactions, making it more reliable for certain types of tests compared to @4tw/cypress-drag-drop.
cypress-drag-drop is another Cypress plugin for drag-and-drop interactions. It offers similar functionality to @4tw/cypress-drag-drop but may have different API methods and options for customization.
A Cypress child command for drag'n'drop support.
Install using npm:
npm install --save-dev @4tw/cypress-drag-drop
or yarn
yarn add --dev @4tw/cypress-drag-drop
Before Cypress is loaded (usually in your commands.js
) put the following line:
require('@4tw/cypress-drag-drop')
Or, if you are using ES module syntax:
import '@4tw/cypress-drag-drop'
This will register the drag
and move
command.
If you're using TypeScript, put the following configuration in a tsconfig.json
{
"compilerOptions": {
"types": ["cypress", "@4tw/cypress-drag-drop"]
}
}
The drag
command is a child command.
The command only accepts elements.
As the drop target simply pass a selector as a string.
In your Cypress spec use the command as follows:
describe('Dragtest', () => {
it('should dragndrop', () => {
cy.visit('/yourpage')
cy.get('.sourceitem').drag('.targetitem')
})
})
Pass the options as an object in the second paramteter.
describe('Dragtest', () => {
it('should dragndrop', () => {
cy.visit('/yourpage')
cy.get('.sourceitem').drag('.targetitem', options)
})
})
During the drag and drop interaction, two elements are involved. The source element being dragged and the drop target.
In order to decide what options should either be applied to the source or the target, the options object can be divided in source
and target
. Options that are not specific to the source or target are applied to both the source and the target.
cy.get('.sourceitem').drag('.target', {
source: { x: 100, y: 100 }, // applies to the element being dragged
target: { position: 'left' }, // applies to the drop target
force: true, // applied to both the source and target element
})
The options are directly passed to Cypress' trigger
command.
So, all options from https://docs.cypress.io/api/commands/trigger#Arguments are possible.
The drag
command is able to tell wether the drag attempt was successful or not. So, the command will yield true
when the drag attempt was successful and false
otherwise:
cy.get('.sourceitem').drag('.target').then((success) => {
assert.isTrue(success)
})
Or you might also want to check that the element is not draggable:
cy.get('.sourceitem').drag('.target').then((success) => {
assert.isFalse(success)
})
The move
command is a child command.
The command only accepts elements.
Define deltaX
and deltaY
as an object parameter to move the element in x- and y-position relative to the element's current position.
In your Cypress spec use the command as follows:
describe('Movetest', () => {
it('should move', () => {
cy.visit('/yourpage')
cy.get('.sourceitem').move({ deltaX: 100, deltaY: 100 })
})
})
The command accepts all options from https://docs.cypress.io/api/commands/trigger#Arguments except position
, x
and y
because they have no effect, since the command makes use of clientX
and clientY
internally.
describe('Movetest', () => {
it('should move', () => {
cy.visit('/yourpage')
cy.get('.sourceitem').move({ deltaX: 100, deltaY: 100, ...options })
})
})
The plugin itself is implemented in the index.js
file.
The tests can be run using Cypress:
yarn test
The test fixtures are under src/examples/
. Using the setExample
Cypress command
the fixture is loaded and ready to run tests on. The first attribute in the setExample
command
is the name of the fixture which needs to be the filename of the component.
cy.setExample('NameOfTheComponent')
Release a new version of this package:
yarn run release
Have a look at the Changelog
[2.2.2] - 2022-11-23
FAQs
A cypress child command for drag'n'drop support.
The npm package @4tw/cypress-drag-drop receives a total of 153,088 weekly downloads. As such, @4tw/cypress-drag-drop popularity was classified as popular.
We found that @4tw/cypress-drag-drop demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.