chore: Refactor RapidMode and add optimization if a test uses DSL (#23338)

Fixes #23264
This commit is contained in:
Rajat Agrawal 2023-05-18 15:38:38 +05:30 committed by GitHub
parent 4527630e4b
commit e6c9a4d0aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 21 deletions

View File

@ -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<string, any>;
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;

View File

@ -32,6 +32,7 @@ const queryLocators = require("../locators/QueryEditor.json");
const welcomePage = require("../locators/welcomePage.json"); const welcomePage = require("../locators/welcomePage.json");
const publishWidgetspage = require("../locators/publishWidgetspage.json"); const publishWidgetspage = require("../locators/publishWidgetspage.json");
import { ObjectsRegistry } from "../support/Objects/Registry"; import { ObjectsRegistry } from "../support/Objects/Registry";
import RapidMode from "./RapidMode";
const propPane = ObjectsRegistry.PropertyPane; const propPane = ObjectsRegistry.PropertyPane;
const agHelper = ObjectsRegistry.AggregateHelper; const agHelper = ObjectsRegistry.AggregateHelper;
@ -607,10 +608,13 @@ Cypress.Commands.add("generateUUID", () => {
}); });
Cypress.Commands.add("addDsl", (dsl) => { Cypress.Commands.add("addDsl", (dsl) => {
let currentURL, pageid, layoutId, appId; let pageid, layoutId, appId;
cy.url().then((url) => { cy.url().then((url) => {
currentURL = url; if (RapidMode.config.enabled && RapidMode.config.usesDSL) {
pageid = currentURL.split("/")[5]?.split("-").pop(); pageid = RapidMode.config.pageID;
} else {
pageid = url.split("/")[5]?.split("-").pop();
}
//Fetch the layout id //Fetch the layout id
cy.request("GET", "api/v1/pages/" + pageid).then((response) => { cy.request("GET", "api/v1/pages/" + pageid).then((response) => {
@ -635,7 +639,12 @@ Cypress.Commands.add("addDsl", (dsl) => {
}).then((response) => { }).then((response) => {
cy.log(response.body); cy.log(response.body);
expect(response.status).equal(200); expect(response.status).equal(200);
cy.reload(); if (RapidMode.config.enabled && RapidMode.config.usesDSL) {
cy.visit(RapidMode.url());
} else {
cy.reload();
}
cy.wait("@getWorkspace"); cy.wait("@getWorkspace");
}); });
}); });

View File

@ -26,6 +26,8 @@ import { initLocalstorage } from "./commands";
import "./dataSourceCommands"; import "./dataSourceCommands";
import "./gitSync"; import "./gitSync";
import { initLocalstorageRegistry } from "./Objects/Registry"; import { initLocalstorageRegistry } from "./Objects/Registry";
import RapidMode from "./RapidMode.ts";
import "./WorkspaceCommands"; import "./WorkspaceCommands";
import "./queryCommands"; import "./queryCommands";
import "./widgetCommands"; import "./widgetCommands";
@ -34,17 +36,6 @@ import "./AdminSettingsCommands";
import "cypress-plugin-tab"; import "cypress-plugin-tab";
/// <reference types="cypress-xpath" /> /// <reference types="cypress-xpath" />
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", () => { Cypress.on("uncaught:exception", () => {
// returning false here prevents Cypress from // returning false here prevents Cypress from
// failing the test // failing the test
@ -58,7 +49,7 @@ Cypress.on("fail", (error) => {
Cypress.env("MESSAGES", MESSAGES); Cypress.env("MESSAGES", MESSAGES);
before(function () { before(function () {
if (rapidMode.enabled) { if (RapidMode.config.enabled) {
cy.startServerAndRoutes(); cy.startServerAndRoutes();
cy.getCookie("SESSION").then((cookie) => { cy.getCookie("SESSION").then((cookie) => {
if (!cookie) { if (!cookie) {
@ -67,13 +58,15 @@ before(function () {
}); });
Cypress.Cookies.preserveOnce("SESSION", "remember_token"); Cypress.Cookies.preserveOnce("SESSION", "remember_token");
cy.visit(rapidMode.url()); if (!RapidMode.config.usesDSL) {
cy.wait("@getWorkspace"); cy.visit(RapidMode.url());
cy.wait("@getWorkspace");
}
} }
}); });
before(function () { before(function () {
if (rapidMode.enabled) { if (RapidMode.config.enabled) {
return; return;
} }
//console.warn = () => {}; //to remove all warnings in cypress console //console.warn = () => {}; //to remove all warnings in cypress console
@ -116,7 +109,7 @@ before(function () {
}); });
before(function () { before(function () {
if (rapidMode.enabled) { if (RapidMode.config.enabled) {
return; return;
} }
//console.warn = () => {}; //console.warn = () => {};
@ -154,7 +147,7 @@ beforeEach(function () {
}); });
after(function () { after(function () {
if (rapidMode.enabled) { if (RapidMode.config.enabled) {
return; return;
} }
//-- Deleting the application by Api---// //-- Deleting the application by Api---//

View File

@ -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. 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. 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? ## 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. **Note:** This can only be done by the project maintainers. Please contact one of them if you require this step to be accomplished.