How to implement drag and drop in cypress test?

后端 未结 6 1393
青春惊慌失措
青春惊慌失措 2020-12-14 07:47

I am struggling to test drag and drop with Cypress and Angular Material Drag and Drop. So the goal is to move \"Get to work\" from Todo to Done. I have created the following

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 08:32

    Here's my cypress command for this:

    Cypress.Commands.add(
      'dragTo',
      (selector: string, position: { x: number; y: number }) => {
        const log = Cypress.log({
          message: `Drag ${selector} to (${position.x}, ${position.y})`,
          consoleProps: () => ({ selector, position })
        });
        log.snapshot('before');
        const ret = cy
          .get(selector, { log: false })
          .trigger('mouseover', { force: true, log: false })
          .trigger('mousedown', {
            button: 0,
            log: false
          })
          .trigger('mousemove', {
            pageX: 10,
            pageY: 10,
            log: false
          })
          .then(el => {
            log.snapshot('Drag start');
            return el;
          })
          .trigger('mousemove', {
            pageX: position.x,
            pageY: position.y,
            force: true,
            log: false
          })
          .then(event => {
            log.snapshot('Drag End');
            return event;
          })
          .trigger('mouseup', { force: true, log: false })
          .then(() => {
            log.snapshot('after');
          });
        log.end();
        return ret;
      }
    );
    

提交回复
热议问题