cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
146 lines (137 loc) • 4.73 kB
JavaScript
;
var yaml = require('yaml');
var fs = require('fs');
var path = require('path');
var debug = require('debug');
var url = require('../url-Ka9JSKx_.js');
require('lodash');
require('semver');
require('ajv');
require('ajv-formats');
require('ajv/lib/refs/json-schema-draft-06.json');
require('date-fns');
require('../util-C7wrzo9A.js');
require('set-cookie-parser');
require('cookie');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var yaml__namespace = /*#__PURE__*/_interopNamespaceDefault(yaml);
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
function readYamlFile(filePath) {
const fileContent = fs__namespace.readFileSync(filePath, "utf-8");
const data = yaml__namespace.parse(fileContent);
return data;
}
function loadConfigFile(filePath, throws = true) {
const log = debug("c8y:scrn:config");
let configData = undefined;
try {
configData = readYamlFile(filePath);
}
catch (error) {
const errorMessage = `Reading ${filePath} failed.\n${error}`;
if (throws) {
throw new Error(errorMessage);
}
else {
console.error(errorMessage);
}
}
if (configData == null) {
log(`Config file is <null>. Skipping.`);
return undefined;
}
log(`Validating config file ${filePath}`);
const schemaMatcher = new url.C8yAjvSchemaMatcher();
const valid = schemaMatcher.ajv.validate(url.schema, configData);
if (!valid) {
const errorMessage = `Invalid config file.\n${schemaMatcher.ajv.errorsText()}`;
if (throws) {
throw new Error(errorMessage);
}
else {
console.error(errorMessage);
}
}
return configData;
}
function createInitConfig(baseUrl) {
return `
# yaml-language-server: $schema=${__dirname}/schema.json
# The title is used to describe the screenshot run
title: "My screenshot automation"
# The baseUrl is the Cumulocity URL and can be overwritten by the command line argument
# All visit URLs are relative to this baseUrl
baseUrl: "${baseUrl}"
global:
viewportWidth: 1440
viewportHeight: 900
language: en
# For user "admin", set environment variables admin_username and admin_password
user: admin
screenshots:
- image: "/my/test/image.png"
visit: "/apps/cockpit/index.html"
tags:
- cockpit
`;
}
function resolveScreenshotFolder(args) {
const screenshotsFolder = path__namespace.resolve(process.cwd(), (typeof args === "string" ? args : args?.folder) ?? "c8yscrn");
if (screenshotsFolder == process.cwd()) {
throw new Error(`Please provide a screenshot folder path that does not resolve to the current working directory.`);
}
return screenshotsFolder;
}
function resolveConfigOptions(args) {
const browser = (args.browser ?? process.env.C8Y_BROWSER ?? "chrome")
.toLowerCase()
.trim();
if (!["chrome", "firefox", "electron"].includes(browser)) {
throw new Error(`Invalid browser ${browser}. Supported browsers are chrome, firefox, electron.`);
}
const screenshotsFolder = resolveScreenshotFolder(args);
const baseUrl = resolveBaseUrl(args);
const cypressFolder = path__namespace.join(path__namespace.dirname(__filename), "cypress");
return {
configFile: path__namespace.resolve(cypressFolder, `config.js`),
browser,
testingType: "e2e",
quiet: args.quiet ?? true,
project: cypressFolder,
config: {
e2e: {
baseUrl,
screenshotOnRunFailure: !(args.skipFailure ?? false),
screenshotsFolder,
trashAssetsBeforeRuns: args.clear ?? false,
spec: path__namespace.join(cypressFolder, "screenshots.cy.js"),
specPattern: path__namespace.resolve(cypressFolder, `*.cy.js`),
},
},
};
}
function resolveBaseUrl(args) {
return url.normalizeBaseUrl(args.baseUrl ?? process.env.C8Y_BASEURL ?? process.env.C8Y_HOST);
}
exports.createInitConfig = createInitConfig;
exports.loadConfigFile = loadConfigFile;
exports.readYamlFile = readYamlFile;
exports.resolveBaseUrl = resolveBaseUrl;
exports.resolveConfigOptions = resolveConfigOptions;
exports.resolveScreenshotFolder = resolveScreenshotFolder;