cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
119 lines (118 loc) • 4.41 kB
JavaScript
const { _ } = Cypress;
import { to_boolean } from "../../shared/util";
/**
* Default selector to wait for when visiting a page. This selector works for different
* Cumulocity versions.
*/
export const C8yVisitDefaultWaitSelector = "c8y-drawer-outlet c8y-app-icon .c8y-icon, c8y-navigator-outlet c8y-app-icon";
Cypress.Commands.add("visitAndWaitForSelector", (url, languageOrOptions, selectorValue, timeoutValue) => {
const DEFAULT_LANGUAGE = "en";
const DEFAULT_TIMEOUT = Cypress.config().pageLoadTimeout || 60000;
const isOptionsObject = (value) => {
return typeof value === "object" && value != null;
};
const options = isOptionsObject(languageOrOptions)
? languageOrOptions
: {
language: languageOrOptions,
selector: selectorValue,
timeout: timeoutValue,
};
const language = options.language ?? DEFAULT_LANGUAGE;
const selector = options.selector ?? C8yVisitDefaultWaitSelector;
const timeout = options.timeout ?? DEFAULT_TIMEOUT;
let remotes = options.remotes ??
Cypress.env("C8Y_SHELL_REMOTES") ??
Cypress.env("C8Y_SHELL_EXTENSION");
if (remotes && typeof remotes === "object") {
remotes = JSON.stringify(remotes);
}
const shell = options.shell ??
Cypress.env("C8Y_SHELL_TARGET") ??
Cypress.env("C8Y_SHELL_NAME");
let forceUrlRemotes = options.forceUrlRemotes ?? Cypress.env("C8Y_SHELL_REMOTES_FORCE");
if (forceUrlRemotes != null && typeof forceUrlRemotes !== "boolean") {
forceUrlRemotes = to_boolean(forceUrlRemotes, false);
}
// Build the final URL with shell target if provided
if (shell) {
url = `/apps/${shell}/index.html#/${url}`;
}
// Log command execution details
const consoleProps = {
url,
language,
selector,
timeout,
shell,
remotes,
forceUrlRemotes,
};
Cypress.log({
name: "visitAndWaitForSelector",
message: url + (remotes ? ` ${remotes}` : ""),
consoleProps: () => consoleProps,
});
cy.setLanguage(language);
const qs = remotes || forceUrlRemotes
? {
qs: {
...(remotes != null && { remotes }),
...(forceUrlRemotes != null && { forceUrlRemotes }),
},
}
: undefined;
cy.visit(url, qs);
cy.get(selector, { timeout }).should("be.visible");
});
Cypress.Commands.add("setLanguage", (lang) => {
// in case only some of the entry points have been imported e.g. only `cumulocity-cypress/commands/general`, then `setLocale` might not be defined
if (typeof globalThis.setLocale === "function") {
globalThis.setLocale(lang);
}
Cypress.log({
name: "setLanguage",
message: lang,
});
cy.intercept({
method: "GET",
url: "/inventory/managedObjects?fragmentType=language*",
}, (req) => {
req.continue((res) => {
const languageFragment = req.query.fragmentType.toString();
if (res.body[languageFragment]) {
res.body[languageFragment] = lang;
}
else if (res.body.managedObjects &&
_.isArrayLike(res.body.managedObjects)) {
res.body.managedObjects.forEach((mo) => {
if (mo[languageFragment]) {
mo[languageFragment] = lang;
}
});
}
res.send();
});
});
window.localStorage.setItem("c8y_language", lang);
Cypress.on("window:before:load", (window) => {
window.localStorage.setItem("c8y_language", lang);
});
});
Cypress.Commands.add("disableGainsight", () => {
Cypress.log({
name: "disableGainsight",
});
cy.intercept("/tenant/system/options/gainsight/api.key*", (req) => {
req.reply({ statusCode: 404, body: {} });
throw new Error("Intercepted Gainsight API key call, but Gainsight should have been disabled. Failing...");
}).as("GainsightAPIKey");
cy.intercept("/tenant/currentTenant*", (req) => {
req.continue((res) => {
const customProperties = res.body.customProperties || {};
customProperties.gainsightEnabled = false;
res.body.customProperties = customProperties;
res.send();
});
});
});