@4tw/cypress-drag-drop

中文文档
2.3.1发布于 2025/09/22GPL-3.0TypeScriptCommonJS零依赖近 30 天下载 61.7万

A cypress child command for drag'n'drop support.

原始使用文档
文档目录4 节
  1. drag
  2. Check command outcome
  3. move
  4. Testing

cypress-drag-drop

A Cypress child command for drag'n'drop support.

Setup

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"]
  }
}

Usage

drag

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.

Check command outcome

The drag command is able to tell whether 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)
})

move

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 })
  })
})

Development

The plugin itself is implemented in the index.js file.

Testing

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

Release a new version of this package:

yarn run release

Changelog

Have a look at the Changelog

版本与发布记录

@4tw/cypress-drag-drop 共发布 22 个版本,首次发布于 2018/12/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.1latest2025/09/221.1 年前
2.3.02025/01/301.8 年前
2.2.52023/09/143 年前
2.2.42023/06/013 年前
2.2.32022/12/123 年前
2.2.22022/11/233 年前

关于 @4tw/cypress-drag-drop 的常见问题

@4tw/cypress-drag-drop 是什么?

A cypress child command for drag'n'drop support.

如何安装 @4tw/cypress-drag-drop?

在项目目录运行 npm install @4tw/cypress-drag-drop,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cypress(>=2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@4tw/cypress-drag-drop 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@4tw/cypress-drag-drop 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@4tw/cypress-drag-drop 可以商用吗?

@4tw/cypress-drag-drop 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@4tw/cypress-drag-drop 的下载量有多少?

根据 npm 官方下载统计,@4tw/cypress-drag-drop 在 2026/10/05 – 2026/11/03 这 30 天内被下载 617,409 次,日均约 20,580 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@4tw/cypress-drag-drop 还在维护吗?

最新版本 2.3.1 发布于 2025/09/22(1.1 年前);至今共发布 22 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/12/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。