diff --git a/app/client/cypress/support/RapidMode.ts b/app/client/cypress/support/RapidMode.ts new file mode 100644 index 0000000000..2b2cd94c75 --- /dev/null +++ b/app/client/cypress/support/RapidMode.ts @@ -0,0 +1,46 @@ +/** + * Sample configuration to be appended to cypress.env.json file + * Documentation on using it is here : https://github.com/appsmithorg/appsmith/blob/release/contributions/docs/TestAutomation.md + * + * "RAPID_MODE": { + "enabled" : true, // Set it to true to enable rapid mode, otherwise set it to false + "appName": "5f8e1666", // Pass your app name here. Given value is a sample value for reference + "pageName": "page-1", // Pass your page name here. Given value is a sample value for reference + "pageID": "64635173cc2cee025a77f489", // Pass your PageID here. Given value is a sample value for reference + "url": "https://dev.appsmith.com/app/5f8e1666/page1-64635173cc2cee025a77f489/edit", // You can choose to pass in url of your app instead of individual parameters above. + "usesDSL": true // Set it to false, if your test doesn't use DSL. If your test uses DSL, you can choose to enable this flag to skip multiple visits to the workspace page. + } + */ +class RapidModeConfig { + config: Record; + static _instance: RapidModeConfig; + + private constructor() { + this.config = Cypress.env("RAPID_MODE") || {}; + } + + static getInstance() { + if (!RapidModeConfig._instance) { + RapidModeConfig._instance = new RapidModeConfig(); + } + + return RapidModeConfig._instance; + } + + url() { + const appName = this.config.appName; + const pageName = this.config.pageName; + const pageID = this.config.pageID; + const parsedURL = this.config.url; + + if (parsedURL.length > 0) { + return parsedURL; + } else { + return `app/${appName}/${pageName}-${pageID}/edit`; + } + } +} + +const RapidMode = RapidModeConfig.getInstance(); + +export default RapidMode; diff --git a/app/client/cypress/support/commands.js b/app/client/cypress/support/commands.js index 2023b2182e..9b147605d5 100644 --- a/app/client/cypress/support/commands.js +++ b/app/client/cypress/support/commands.js @@ -32,6 +32,7 @@ const queryLocators = require("../locators/QueryEditor.json"); const welcomePage = require("../locators/welcomePage.json"); const publishWidgetspage = require("../locators/publishWidgetspage.json"); import { ObjectsRegistry } from "../support/Objects/Registry"; +import RapidMode from "./RapidMode"; const propPane = ObjectsRegistry.PropertyPane; const agHelper = ObjectsRegistry.AggregateHelper; @@ -607,10 +608,13 @@ Cypress.Commands.add("generateUUID", () => { }); Cypress.Commands.add("addDsl", (dsl) => { - let currentURL, pageid, layoutId, appId; + let pageid, layoutId, appId; cy.url().then((url) => { - currentURL = url; - pageid = currentURL.split("/")[5]?.split("-").pop(); + if (RapidMode.config.enabled && RapidMode.config.usesDSL) { + pageid = RapidMode.config.pageID; + } else { + pageid = url.split("/")[5]?.split("-").pop(); + } //Fetch the layout id cy.request("GET", "api/v1/pages/" + pageid).then((response) => { @@ -635,7 +639,12 @@ Cypress.Commands.add("addDsl", (dsl) => { }).then((response) => { cy.log(response.body); expect(response.status).equal(200); - cy.reload(); + if (RapidMode.config.enabled && RapidMode.config.usesDSL) { + cy.visit(RapidMode.url()); + } else { + cy.reload(); + } + cy.wait("@getWorkspace"); }); }); diff --git a/app/client/cypress/support/index.js b/app/client/cypress/support/index.js index 54fa6c5f06..9e39d8a077 100644 --- a/app/client/cypress/support/index.js +++ b/app/client/cypress/support/index.js @@ -26,6 +26,8 @@ import { initLocalstorage } from "./commands"; import "./dataSourceCommands"; import "./gitSync"; import { initLocalstorageRegistry } from "./Objects/Registry"; +import RapidMode from "./RapidMode.ts"; + import "./WorkspaceCommands"; import "./queryCommands"; import "./widgetCommands"; @@ -34,17 +36,6 @@ import "./AdminSettingsCommands"; import "cypress-plugin-tab"; /// -let rapidMode = { - enabled: false, // Set to true to disable app creation - appName: "cf023e29", // Replace it with your app name - pageName: "page1", // Replace it with the page name - pageID: "644d0ec870cec01248edfc9a", // Replace it with pageID - - url: function () { - return `app/${this.appName}/${this.pageName}-${this.pageID}/edit`; - }, -}; - Cypress.on("uncaught:exception", () => { // returning false here prevents Cypress from // failing the test @@ -58,7 +49,7 @@ Cypress.on("fail", (error) => { Cypress.env("MESSAGES", MESSAGES); before(function () { - if (rapidMode.enabled) { + if (RapidMode.config.enabled) { cy.startServerAndRoutes(); cy.getCookie("SESSION").then((cookie) => { if (!cookie) { @@ -67,13 +58,15 @@ before(function () { }); Cypress.Cookies.preserveOnce("SESSION", "remember_token"); - cy.visit(rapidMode.url()); - cy.wait("@getWorkspace"); + if (!RapidMode.config.usesDSL) { + cy.visit(RapidMode.url()); + cy.wait("@getWorkspace"); + } } }); before(function () { - if (rapidMode.enabled) { + if (RapidMode.config.enabled) { return; } //console.warn = () => {}; //to remove all warnings in cypress console @@ -116,7 +109,7 @@ before(function () { }); before(function () { - if (rapidMode.enabled) { + if (RapidMode.config.enabled) { return; } //console.warn = () => {}; @@ -154,7 +147,7 @@ beforeEach(function () { }); after(function () { - if (rapidMode.enabled) { + if (RapidMode.config.enabled) { return; } //-- Deleting the application by Api---// diff --git a/contributions/docs/TestAutomation.md b/contributions/docs/TestAutomation.md index 06b681d8b4..fad7c37cdc 100644 --- a/contributions/docs/TestAutomation.md +++ b/contributions/docs/TestAutomation.md @@ -41,6 +41,27 @@ If you want to add a new env variable to cypress tests, add it to the `cypress.env.json` file and also in the documentation above. All ENV variables from your `.env` file and all `APPSMITH_*` env variables from `process.env` are accessible with the `Cypress.env()` method. + +## Speeding up debugging/writing tests + +- The test suite has a flag to enable rapid mode, which skips a few test environment setup steps if it is already setup. +- This speeds up the execution of the test run and is helpful during debugging and writing tests. Some of the steps that it skips are, + - Creation of a new test app everytime. We can pass an app id to the test so that it can reuse it and avoid creating a new app everytime. + - Skip login if the user is already logged in from previous test run session. + - Skip multiple visit to the workspace page if a test uses DSL for loading fixtures. If a test uses DSL, a visit to the workspace is mandatory. Thus avoiding multiple visits to the workspace page saves time during test run. + - To enable rapid mode for your test, you can add following configuration to your `cypress.env.json` file created above, +``` + "RAPID_MODE": { + "enabled" : true, // Set it to true to enable rapid mode, otherwise set it to false + "appName": "5f8e1666", // Pass your app name here. Given value is a sample value for reference + "pageName": "page-1", // Pass your page name here. Given value is a sample value for reference + "pageID": "64635173cc2cee025a77f489", // Pass your PageID here. Given value is a sample value for reference + "url": "https://dev.appsmith.com/app/5f8e1666/page1-64635173cc2cee025a77f489/edit", // You can choose to pass in url of your app instead of individual parameters above. + "usesDSL": true // Set it to false, if your test doesn't use DSL. If your test uses DSL, you can choose to enable this flag to skip multiple visits to the workspace page. + } +``` +- You can either pass in complete url for your app in the test or pass in parameters for your app and the url will be generated on its own. + ## How do I add environment variables required for Cypress tests? **Note:** This can only be done by the project maintainers. Please contact one of them if you require this step to be accomplished.