@4tw/cypress-drag-drop 中文文档教程

发布于 3年前 浏览 29 项目主页 更新于 3年前

cypress-drag-drop

用于拖放支持的 Cypress 子命令。

Setup

使用 npm:

npm install --save-dev @4tw/cypress-drag-drop

或 yarn

yarn add --dev @4tw/cypress-drag-drop

安装在加载 Cypress 之前(通常在您的 commands.js 中)放置以下行:

require('@4tw/cypress-drag-drop')

或者,如果您使用的是 ES 模块语法:

import '@4tw/cypress-drag-drop'

这将注册 drag 和 move 命令。

如果您使用的是 TypeScript,请将以下配置放入 tsconfig.json

{
  "compilerOptions": {
    "types": ["cypress", "@4tw/cypress-drag-drop"]
  }
}

Usage

drag

drag 命令是一个子命令。 该命令只接受元素。 作为放置目标,只需将选择器作为字符串传递。

在您的赛普拉斯规范中,使用如下命令:

describe('Dragtest', () => {
  it('should dragndrop', () => {
    cy.visit('/yourpage')

    cy.get('.sourceitem').drag('.targetitem')
  })
})

将选项作为对象传递到第二个参数中。

describe('Dragtest', () => {
  it('should dragndrop', () => {
    cy.visit('/yourpage')

    cy.get('.sourceitem').drag('.targetitem', options)
  })
})

在拖放交互过程中,涉及两个元素。 被拖动的源元素和放置目标。 为了决定应该将哪些选项应用于源或目标,可以将选项对象分为 sourcetarget。 不特定于源或目标的选项同时应用于源和目标。

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

这些选项直接传递给赛普拉斯的 trigger 命令。 因此,https://docs.cypress.io/api/commands/trigger#Arguments 中的所有选项都是可能的。

Check command outcome

drag 命令能够判断拖动尝试是否成功。 因此,当拖动尝试成功时,该命令将产生 true,否则将产生 false

cy.get('.sourceitem').drag('.target').then((success) => {
  assert.isTrue(success)
})

或者您可能还想检查元素是否不可拖动:

cy.get('.sourceitem').drag('.target').then((success) => {
  assert.isFalse(success)
})

move

move 命令是一个子命令。 该命令只接受元素。 将 deltaXdeltaY 定义为对象参数,以相对于元素的当前位置在 x 和 y 位置移动元素。

在您的 Cypress 规范中使用如下命令:

describe('Movetest', () => {
  it('should move', () => {
    cy.visit('/yourpage')

    cy.get('.sourceitem').move({ deltaX: 100, deltaY: 100 })
  })
})

该命令接受来自 https://docs.cypress.io/api/commands/trigger#Arguments 的所有选项,positionxy 因为它们没有效果,因为该命令在内部使用了 clientXclientY

describe('Movetest', () => {
  it('should move', () => {
    cy.visit('/yourpage')

    cy.get('.sourceitem').move({ deltaX: 100, deltaY: 100, ...options })
  })
})

Development

插件本身在 index.js 文件中实现。

Testing

可以使用 Cypress 运行测试:

yarn test

测试装置位于 src/examples/ 下。 使用 setExample Cypress 命令 夹具已加载并准备好运行测试。 setExample 命令中的第一个属性 是夹具的名称,它需要是组件的文件名。

cy.setExample('NameOfTheComponent')

Release

发布此软件包的新版本:

yarn run release

Changelog

查看 Changelog

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

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

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文