因此,我使用 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/