javascript - 如何从 Cypress 返回 Map 对象?

标签 javascript node.js automated-tests cypress

因此,我使用 Cypress 编写 TAF 来自动化用户案例。我是这方面的新手。

我需要从 Cypress each 命令返回一个 Map 以及一些值,以便在下一个命令中将其用作输入值。

在 DOM 中,有一些像这样的 canvas 标签:

<canvas class="leaflet-tile leaflet-tile-loaded" width="256" height="256" style="width: 256px; height: 256px; transform: translate3d(613px, 246px, 0px); opacity: 1;"></canvas>

style 属性中,我需要提取两个坐标,因此,只需转换值:

width: 256px; height: 256px; transform: translate3d(613px, 246px, 0px); opacity: 1;

613 246

并将其设置为 Map 对象的键。作为值,我需要设置编码 Canvas 数据的缓冲区。

所以,我添加这样的自定义命令:

function convertCanvasMatrixToPictureCommand() {
  Cypress.Commands.add('renderCanvasMatrixToPng', { prevSubject: true }, (subject, savePath) => {
    const bufferMap = new Map();
    cy.wrap(subject)
      .each(canvas => {
        Cypress.Blob.canvasToBlob(canvas.get(0))
          .then(blob => Cypress.Blob.blobToArrayBuffer(blob))
          .then(buff => {
            const coordinates = extract(canvas.attr('style'));
            const buffer = Buffer.from(buff);
            bufferMap.set(coordinates, buffer);
          });
      // and here in some way I need to return bufferMap obj
      // to use it as input data in next cypress task:
      })
      .task('mergeImages', { buffers: bufferMap, savePath: 'cypress/snapshots' });
  });
}

mergeImages 任务将继续处理 map 并使用指定的排序,将所有 Canvas 合并为单个 PNG 图像。

但是是否可以通过某种方式从每个命令返回该 map ?

bufferMap 对象仅在每个命令内有效。但每个仍然是空的

cy.wprap() 也无法解决此问题。或者我用错了...

谢谢!

最佳答案

自定义命令的几个问题

  • 为了确保等待 .each() 的结果,必须返回由 Cypress.Blob.canvasToBlob() 链创建的 Promise。

  • .each() 后面加上 .then() 以确保完成,并在此处返回`bufferMap。

.task() 的问题

  • 它不喜欢在自定义命令中调用,因此请在之后调用

    CypressError: cy.then() failed because you are mixing up async and sync code.

  • 它不喜欢Map()对象作为参数,转换为普通对象

测试

describe('leaflet', () => {

  it('processes', () => {

    Cypress.Commands.add('renderCanvasMatrixToPng', {prevSubject: true}, (subjects, savePath) => {
      const bufferMap = new Map();
      cy.wrap(subjects)
        .each((canvas, i) => {
          return Cypress.Blob.canvasToBlob(canvas.get(0))   // <- add return here
            .then(blob => Cypress.Blob.blobToArrayBuffer(blob))
            .then(buff => {
              var view = new Int8Array(buff);  // not sure why this is needed
              const buffer = Buffer.from(view);

              // get coords here
              const coords = 1000 + i // for purpose of demo
              bufferMap.set(coords, buffer)
            })
        })
        .then(_ => {                           // <- wait for previous to complete
          console.log('bufferMap inside command', bufferMap)  // [[Entries]]
                                                              //  0: {0 => Uint8Array(27209)}
                                                              //  1: {1 => Uint8Array(1179)}
          return bufferMap;
        })
    });

    cy.visit('http://cartodb.github.io/Leaflet.CanvasLayer/example.html')

    cy.get('canvas').renderCanvasMatrixToPng().then(bufferMap => {
      console.log('bufferMap outside command', bufferMap)  // [[Entries]]
                                                           //    0: {1000 => Uint8Array(25218)}
                                                           //    1: {1001 => Uint8Array(1179)}
      const asObject = Object.fromEntries(bufferMap);
      cy.task('mergeImages', { buffers: asObject, savePath: 'cypress/snapshots' });
    })

  })
})

演示任务

module.exports = (on, config) => {
  ...
  on('task', {
    mergeImages(options) {
      const { buffers, savePath } = options;

      console.log('buffers', buffers);
      /* In terminal

        buffers {
          '1000': {
            type: 'Buffer',
            data: [
              137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,
              ... 33137 more items
            ]
          },
          '1001': {
            type: 'Buffer',
            data: [
              137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,
              ... 1079 more items
            ]
          }
        }
      */

      return null;
    }
  })
}

替代命令(我的偏好)

Cypress.Commands.add('renderCanvasMatrixToPng', { prevSubject: true }, (subjects, savePath) => {
  const bufferPromises = Array.from(subjects).map(canvas => {
    return Cypress.Blob.canvasToBlob(canvas)
      .then(blob => Cypress.Blob.blobToArrayBuffer(blob))
      .then(buff => {
        const view = new Int8Array(buff);
        const buffer = Buffer.from(view);
        return buffer;
      })
  })
  return Promise.all(bufferPromises).then(buffers => {
    const bufferMap = new Map();
    buffers.forEach((buffer, i) => {
      // get coords here
      const coords = 1000 + i // for purpose of demo
      bufferMap.set(coords, buffer)
    })
    return bufferMap;
  })
});

关于javascript - 如何从 Cypress 返回 Map 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260360/

相关文章:

javascript - Bootstrap标签输入不适用于本地数据

javascript - MongoDb - 具有空值的有界数组显示无界

javascript - 使用 MEAN angularjs Nodejs Stack 的 Phonegap 客户端

automated-tests - Karate ;单个场景中的多个 url

javascript - Bootstrap 滑动菜单 navbar-right

node.js - AWS Lambda TooManyRequestsException : Rate Exceeded

javascript - 动态实例化对象(Mistic 查询生成器)

javascript - Express.js 从外部 API 渲染数据

javascript - 如何使用 selenium 驱动程序调整表中的列宽

java - 如何在 @test.enabled 属性中给出运行时值