When I executed tests (13.13.0) with this command snapshotIframeContents. I have error cy.task() must only be invoked from the spec file or support file.
Worked in version 12.3.0
Desired behavior
No response
Test code to reproduce
cypress\support\commands.ts
Cypress.Commands.add("snapshotIframeContents", { prevSubject: "element" }, (subject) => {
return cy
.wrap(subject[0])
.its("0.contentWindow.document.body.innerHTML")
.then(async (innerHTML: string) => {
const wrapper = document.createElement("div");
wrapper.innerHTML = innerHTML;
// Remove style since its compilation differs between enviroments
Array.from(wrapper.querySelectorAll("style")).forEach((element) => element.remove());
// Reduce base64 image size
await Promise.all(
Array.from(wrapper.querySelectorAll("img")).map(async (element) => {
if (/data:image\//.exec(element.src)) {
// Taken from: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest, there is no nicer way of doing this
const msgUint8 = new TextEncoder().encode(element.src); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string
element.src = hashHex;
}
}),
);
cy.wrap(wrapper).toMatchSnapshot(); //problem here
});
});
also tried this variant with spy:
Cypress.Commands.add("snapshotIframeContents", { prevSubject: "element" }, (subject) => {
const callbackSpy = cy.spy().as("callbackSpy");
cy.wrap(subject[0])
.its("0.contentWindow.document.body.innerHTML")
.then(async (innerHTML: string) => {
const wrapper = document.createElement("div");
wrapper.innerHTML = innerHTML;
// Remove style since its compilation differs between enviroments
Array.from(wrapper.querySelectorAll("style")).forEach((element) => element.remove());
// Reduce base64 image size
await Promise.all(
Array.from(wrapper.querySelectorAll("img")).map(async (element) => {
if (/data:image\//.exec(element.src)) {
// Taken from: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest, there is no nicer way of doing this
const msgUint8 = new TextEncoder().encode(element.src); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string
element.src = hashHex;
}
}),
);
callbackSpy();
});
cy.get("@callbackSpy").should("be.called");
cy.wrap(subject[0]).its("0.contentWindow.document.body.innerHTML").toMatchSnapshot();
});
calling it:
EhicReissue\ehicReissueFormCertificate.spec.ts
cy.log("8. Vyzkoušej tlačítko pro tisk");
cy.get("[data-testid='newborn-certificate-printer']").snapshotIframeContents();
Current behavior
When I executed tests (13.13.0) with this command snapshotIframeContents. I have error cy.task() must only be invoked from the spec file or support file.
Worked in version 12.3.0
Desired behavior
No response
Test code to reproduce
cypress\support\commands.ts
also tried this variant with spy:
calling it:
EhicReissue\ehicReissueFormCertificate.spec.ts
Cypress Version
13.13.0
Node version
18.20.3
Operating System
Windows 10 Pro
Debug Logs
No response
Other
No response