我有一些固定装置来存根用 protobuf 编码消息的服务器(我正在使用protobufjs
)。我想对固定装置进行解码以轻松操作它们并让赛普拉斯在将响应发送给客户端之前对存根主体进行编码,我该怎么做?
问问题
583 次
1 回答
1
[更新] 现在可以作为赛普拉斯插件使用
这就是我的解决方案:
cypress/plugins/protobufjs/index.js
文件(导入 protobuf 定义的地方)
const path = require("path");
const protobufjs = require("protobufjs");
const definition = path.join(__dirname, "../../../public/escrow/ui.proto");
const proto = protobufjs.loadSync(definition);
module.exports = {
Status: proto.lookupType("escrow.Status"),
};
cypress/plugins/index.js
文件(通过自定义赛普拉斯任务进行编码)
const { StringDecoder } = require("string_decoder");
const Messages = require("./protobufjs");
module.exports = on => {
on("task", {
protobufEncode: ({ data, encoderName }) => {
const decoder = new StringDecoder("utf8");
const bufferValue = Messages[encoderName].encode(data).finish();
return decoder.end(Buffer.from(bufferValue));
}
});
};
- 在你的测试中
cy.fixture("YOUR_FIXTURE.json").then(async json => {
cy.task("protobufEncode", { encoderName: "Status", data: json }).then(result => {
cy.route({
headers: {
"content-type": "application/octet-stream"
},
method: "GET",
response: result,
status: 200,
url: `**/YOUR_URL`
}).as("route_status_one_usb_key");
});
});
于 2019-02-11T11:07:10.063 回答