Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cy.task() must only be invoked from the spec file or support file #29899

Open
Pavel4444 opened this issue Jul 22, 2024 · 1 comment
Open

cy.task() must only be invoked from the spec file or support file #29899

Pavel4444 opened this issue Jul 22, 2024 · 1 comment
Labels
stage: needs investigating Someone from Cypress needs to look at this

Comments

@Pavel4444
Copy link

Pavel4444 commented Jul 22, 2024

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

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();

Cypress Version

13.13.0

Node version

18.20.3

Operating System

Windows 10 Pro

Debug Logs

No response

Other

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this
Projects
None yet
Development

No branches or pull requests

3 participants