PromucFlow_constructor/app/client/cypress/plugins/index.js
Saroj 9895ee217e
ci: Split spec improvements for cypress ci runs (#26774)
> Pull Request Template
>
> Use this template to quickly create a well written pull request.
Delete all quotes before creating the pull request.
>
## Description
> Add a TL;DR when description is extra long (helps content team)
>
> Please include a summary of the changes and which issue has been
fixed. Please also include relevant motivation
> and context. List any dependencies that are required for this change
>
> Links to Notion, Figma or any other documents that might be relevant
to the PR
>
>
#### PR fixes following issue(s)
Fixes # (issue number)
> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-09-25 09:49:21 +05:30

225 lines
6.5 KiB
JavaScript

/// <reference types="cypress" />
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
const chalk = require("chalk");
// const _ = require("lodash");
// const del = require("del");
const cypressLogToOutput = require("cypress-log-to-output");
//const { isFileExist } = require("cy-verify-downloads");
const {
addMatchImageSnapshotPlugin,
} = require("cypress-image-snapshot/plugin");
const { tagify } = require("cypress-tags");
const { cypressHooks } = require("../scripts/cypress-hooks");
const { cypressSplit } = require("../scripts/cypress-split");
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
/**
* @type {Cypress.PluginConfig}
*/
module.exports = async (on, config) => {
// on("task", {
// isFileExist,
// });
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
cypressLogToOutput.install(on, (type, event) => {
if (event.level === "error" || event.type === "error") {
return true;
}
return false;
});
on("file:preprocessor", tagify(config));
addMatchImageSnapshotPlugin(on, config);
on("before:browser:launch", (browser = {}, launchOptions) => {
/*
Uncomment below to get console log printed in cypress output
*/
launchOptions.args = cypressLogToOutput.browserLaunchHandler(
browser,
launchOptions.args,
);
if (browser.name === "chrome") {
const video = path.join(
"cypress",
"fixtures",
"Videos",
"webCamVideo.y4m",
);
launchOptions.args.push("--disable-dev-shm-usage");
launchOptions.args.push("--window-size=1400,1100");
launchOptions.args.push("--use-fake-ui-for-media-stream");
launchOptions.args.push("--use-fake-device-for-media-stream");
//Stream default video source for camera & code scanner
launchOptions.args.push(`--use-file-for-fake-video-capture=${video}`);
return launchOptions;
}
if (browser.name === "electron") {
// && browser.isHeadless) {
launchOptions.preferences.fullscreen = true;
launchOptions.preferences.darkTheme = true;
launchOptions.preferences.width = 1400;
launchOptions.preferences.height = 1100;
launchOptions.preferences.resizable = false;
return launchOptions;
}
});
// module.exports = (on, config) => {
// on("after:spec", (spec, results) => {
// if (results && results.video) {
// // Do we have failures for any retry attempts?
// const failures = _.some(results.tests, (test) => {
// return _.some(test.attempts, { state: "failed" });
// });
// if (!failures) {
// // delete the video if the spec passed and no tests retried
// return del(results.video);
// }
// }
// });
// };
/**
* Fallback to APPSMITH_* env variables for Cypress.env if config.env doesn't already have it.
* Note: APPSMITH_* ENV vars have lower precedence than *all* methods mentioned in https://docs.cypress.io/guides/guides/environment-variables.html
* Example #1:
* process.env -> APPSMITH_FOO=bar
* cypress.json -> APPSMITH_FOO=baz
*
* Cypress.env("APPSMITH_FOO") // baz
*
* Example #2:
* process.env -> APPSMITH_FOO=bar
* cypress.json -> APPSMITH_FOO=
*
* Cypress.env("APPSMITH_FOO") // <empty>
*/
Object.keys(process.env).forEach((key) => {
if (
key.startsWith("APPSMITH_") &&
!Object.prototype.hasOwnProperty.call(config.env, key)
) {
config.env[key] = process.env[key];
}
});
/**
* Fallback to .env variables for Cypress.env if procecss.env doesn't have it either
* Note: Value in .env file has the lowest precedence, even lower than APPSMITH_* ENV vars.
* Example:
* .env -> APPSMITH_FOO=bar
* process.env -> APPSMITH_FOO=
*
* Cypress.env("APPSMITH_FOO") // <empty>
*/
try {
const parsedEnv = dotenv.parse(
fs.readFileSync(path.join(__dirname, "../../../../.env"), {
encoding: "utf-8",
}),
);
Object.keys(parsedEnv).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(config.env, key)) {
config.env[key] = parsedEnv[key];
}
});
} catch (e) {
console.error(
chalk.yellow(
"\n====================================================================================================\n" +
chalk.red(e.message) +
"\n\n" +
"Could not load env variables from .env file, make sure you have one!\n" +
"====================================================================================================\n",
),
);
}
/**
* This task logs the message on the CLI terminal. Use with care because it can log sensitive details
* Example usage: cy.task('log', 'This is the message printed to the terminal')
*/
on("task", {
log(message) {
console.log(message);
return null;
},
/*
Change video source for for camera & code scanner
*/
changeVideoSource(videoSource) {
console.log("TASK - Changing video source to", videoSource);
const webcamPath = path.join(
"cypress",
"fixtures",
"Videos",
"webCamVideo.y4m",
);
const defaultVideoPath = path.join(
"cypress",
"fixtures",
"Videos",
videoSource,
);
const video = fs.readFileSync(defaultVideoPath);
fs.writeFile(webcamPath, video);
return null;
},
/*
Reset video source to default
*/
resetVideoSource() {
console.log("TASK - Resetting video source");
const webcamPath = path.join(
"cypress",
"fixtures",
"Videos",
"webCamVideo.y4m",
);
const defaultVideoPath = path.join(
"cypress",
"fixtures",
"Videos",
"defaultVideo.y4m",
);
const video = fs.readFileSync(defaultVideoPath);
fs.writeFile(webcamPath, video);
return null;
},
});
if (process.env["RUNID"]) {
config = await new cypressSplit().splitSpecs(on, config);
cypressHooks(on, config);
}
return config;
};