I want to run a Cypress test case within a web worker to avoid relying on API, which would reduce deployment costs in a serverless environment. My main challenge is figuring out how to import Cypress modules and make its commands available in the web worker context. Below is the test case I’m aiming to run.
it("should visit all the URLs and check thier status", () => {
cy.get("@URLs").each((url) => {
cy.visit(url, { failOnStatusCode: false }).then(() => {
cy.get('img[src="/images/en.svg"]').click();
cy.get("body").then(($body) => {
cy.wait(4000).then(() => {
if (
$body.find("div.css-t9xer0").length > 0 ||
$body.find("div.css-1ceac07").length > 0
) {
cy.get("h2.chakra-heading.css-18j379d").then((element) => {
const text = element.text();
testResults.push({
url,
status: text.includes("Standard QR is valid")
? "passed"
: "failed",
});
});
} else if ($body.find("div.css-7htuvi").length > 0) {
cy.get("p.chakra-text.css-mhu0er").then((element) => {
const text = element.text();
testResults.push({
url,
status: text.includes("QR code not found")
? "QR code not found"
: text,
});
});
} else {
testResults.push({
url,
status: "QR code has content not expected.",
});
}
});
});
});
});
});
I tried this but it didn’t work.
import cypress from "cypress";
// const cypress = require("cypress");
self.onmessage = async function (e) {
const { data, status, specs, config } = e.data;
if (status === "start") {
try {
// const results = await cypress.run({
// spec: specs,
// config: {
// ...config,
// video: false,
// screenshotOnRunFailure: false,
// },
// });
// console.log(results);
self.postMessage({ status: "done", data: results });
} catch (error) {
self.postMessage({ status: "error", error: error.message });
}
}
};