From f322594b38a51c12f4d3dd891696170e8c06f2c8 Mon Sep 17 00:00:00 2001 From: "preethi.m" Date: Fri, 18 Oct 2024 16:32:40 +0530 Subject: [PATCH 1/5] performance metrics collection support using beforeeach/aftereach --- cypress/support/constants/constants.js | 1 + cypress/support/cypress-commands/commands.js | 3 ++ cypress/support/cypress-support/src/main.js | 52 +++++++++++++++---- cypress/support/validations/index.js | 1 + .../validations/performanceValidation.js | 46 ++++++++++++++++ 5 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 cypress/support/validations/performanceValidation.js diff --git a/cypress/support/constants/constants.js b/cypress/support/constants/constants.js index 436b07d4..3e8f1158 100644 --- a/cypress/support/constants/constants.js +++ b/cypress/support/constants/constants.js @@ -499,6 +499,7 @@ module.exports = { SET_EVENT_SUCCESS: 'Event value set successfully in platform', SCENARIO_NAME: 'scenarioName', FIREBOLT_INTERACTION: 'FireboltInteraction', + PERFORMANCE_VALIDATION: 'performanceValidation', }; function getSanityReportPath() { // Check if Cypress is defined, for cypress test context diff --git a/cypress/support/cypress-commands/commands.js b/cypress/support/cypress-commands/commands.js index becd5c6e..1eb38d05 100644 --- a/cypress/support/cypress-commands/commands.js +++ b/cypress/support/cypress-commands/commands.js @@ -1354,6 +1354,9 @@ Cypress.Commands.add('methodOrEventResponseValidation', (validationType, request case CONSTANTS.UNDEFINED: cy.undefinedValidation(object, methodOrEventObject, validationType); break; + case CONSTANTS.PERFORMANCE_VALIDATION: + cy.performanceValidation(object); + break; default: assert(false, 'Unsupported validation type'); break; diff --git a/cypress/support/cypress-support/src/main.js b/cypress/support/cypress-support/src/main.js index cc22e41e..dcc520c6 100644 --- a/cypress/support/cypress-support/src/main.js +++ b/cypress/support/cypress-support/src/main.js @@ -89,15 +89,6 @@ export default function (module) { Cypress.env(CONSTANTS.MESSAGE_QUEUE, messageQueue); UTILS.parseExceptionList(); cy.getModuleReqIdJson(); - if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) == true) { - cy.startOrStopPerformanceService(CONSTANTS.INITIATED).then((response) => { - if (response) { - Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, true); - } - }); - } else { - cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); - } // Merge fireboltCalls const v1FireboltCallsData = UTILS.getEnvVariable('fireboltCallsJson'); @@ -132,8 +123,36 @@ export default function (module) { // beforeEach beforeEach(() => { UTILS.getEnvVariable(CONSTANTS.FB_INTERACTIONLOGS).clearLogs(); - cy.getBeforeOperationObject(); - UTILS.destroyGlobalObjects([CONSTANTS.LIFECYCLE_APP_OBJECT_LIST]); + + cy.getBeforeOperationObject().then(() => { + const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); + + UTILS.destroyGlobalObjects([CONSTANTS.LIFECYCLE_APP_OBJECT_LIST]); + + if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) === true) { + if (scenarioName) { + const requestMap = { + method: CONSTANTS.REQUEST_OVERRIDE_CALLS.CREATE_MARKER, + params: scenarioName, + }; + fireLog.info(`Firebolt Call to 1st party App: ${JSON.stringify(requestMap)} `); + cy.sendMessagetoPlatforms(requestMap).then((result) => { + fireLog.isTrue( + result.success, + 'Response for marker creation: ' + JSON.stringify(result) + ); + }); + + cy.startOrStopPerformanceService(CONSTANTS.INITIATED).then((response) => { + if (response) { + Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, true); + } + }); + } + } else { + cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); + } + }); }); /** @@ -242,6 +261,17 @@ export default function (module) { })(); }); + //after each + afterEach(() => { + if (UTILS.getEnvVariable(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, false) == true) { + cy.startOrStopPerformanceService(CONSTANTS.STOPPED).then((response) => { + if (response) { + Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, false); + } + }); + } + }); + /** * @module main * @function sendMessagetoPlatforms diff --git a/cypress/support/validations/index.js b/cypress/support/validations/index.js index c60597d8..903e9da5 100644 --- a/cypress/support/validations/index.js +++ b/cypress/support/validations/index.js @@ -20,3 +20,4 @@ import './decodeValidation'; import './regExValidation'; import './schemaValidation'; import './undefinedValidation'; +import './performanceValidation'; diff --git a/cypress/support/validations/performanceValidation.js b/cypress/support/validations/performanceValidation.js new file mode 100644 index 00000000..49ea6913 --- /dev/null +++ b/cypress/support/validations/performanceValidation.js @@ -0,0 +1,46 @@ +/** + * Copyright 2024 Comcast Cable Communications Management, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +const CONSTANTS = require('../constants/constants'); +const UTILS = require('../cypress-support/src/utils'); + +Cypress.Commands.add('performanceValidation', (object) => { + if (UTILS.getEnvVariable('performanceMetrics')) { + const { type, process, percentile, threshold } = object.validations[0]; + const requestMap = { + method: CONSTANTS.REQUEST_OVERRIDE_CALLS.PERFORMANCE_THRESHOLD_VALIDATOR, + params: { type, process, percentile, threshold }, + }; + + cy.sendMessagetoPlatforms(requestMap).then((result) => { + if (result.error) { + cy.log('Failed to fetch and validate the performance metrics').then(() => { + assert(false, result.error); + }); + } else { + result.map((response) => { + cy.log(response.message).then(() => { + assert.equal(true, response?.success, response?.message); + }); + }); + } + }); + } else { + fireLog.info('performanceMetrics are disabled.'); + } +}); From f689225bb757a6e0a390da89afb1d00d02756b48 Mon Sep 17 00:00:00 2001 From: "preethi.m" Date: Fri, 18 Oct 2024 17:16:01 +0530 Subject: [PATCH 2/5] minor change --- cypress/support/cypress-support/src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/support/cypress-support/src/main.js b/cypress/support/cypress-support/src/main.js index dcc520c6..006ee084 100644 --- a/cypress/support/cypress-support/src/main.js +++ b/cypress/support/cypress-support/src/main.js @@ -261,7 +261,7 @@ export default function (module) { })(); }); - //after each + // after each afterEach(() => { if (UTILS.getEnvVariable(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, false) == true) { cy.startOrStopPerformanceService(CONSTANTS.STOPPED).then((response) => { From 2fb4aaaef7b869ed1d65f4aef7b48e82861f322d Mon Sep 17 00:00:00 2001 From: "preethi.m" Date: Fri, 25 Oct 2024 14:58:54 +0530 Subject: [PATCH 3/5] performance changes --- cypress/support/cypress-commands/commands.js | 24 +++++++++-- cypress/support/cypress-support/src/main.js | 43 ++----------------- .../validations/performanceValidation.js | 5 ++- 3 files changed, 27 insertions(+), 45 deletions(-) diff --git a/cypress/support/cypress-commands/commands.js b/cypress/support/cypress-commands/commands.js index 578f9c7a..bb42752c 100644 --- a/cypress/support/cypress-commands/commands.js +++ b/cypress/support/cypress-commands/commands.js @@ -781,10 +781,6 @@ Cypress.Commands.add('parsedMockData', (beforeOperation) => { * cy.startOrStopPerformanceService('stopped') */ Cypress.Commands.add('startOrStopPerformanceService', (action) => { - if (action == CONSTANTS.INITIATED) { - const epochTime = Number.parseInt(Date.now() / 1000); - Cypress.env(CONSTANTS.THRESHOLD_MONITOR_START_TIME, epochTime); - } const requestMap = { method: CONSTANTS.REQUEST_OVERRIDE_CALLS.SETPERFORMANCETESTHANDLER, params: { @@ -1547,3 +1543,23 @@ Cypress.Commands.add('startOrStopInteractionsService', (action) => { Cypress.Commands.add('envConfigSetup', () => { fireLog.info('No additional config module environment setup'); }); + +Cypress.Commands.add('initiatePerformanceMetrics', () => { + const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); + + if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) === true) { + const requestMap = { + method: CONSTANTS.REQUEST_OVERRIDE_CALLS.CREATE_MARKER, + params: scenarioName, + }; + fireLog.info(`Firebolt Call to 1st party App: ${JSON.stringify(requestMap)} `); + cy.sendMessagetoPlatforms(requestMap).then((result) => { + fireLog.isTrue(result.success, 'Response for marker creation: ' + JSON.stringify(result)); + }); + + const epochTime = Number.parseInt(Date.now() / 1000); + Cypress.env(CONSTANTS.THRESHOLD_MONITOR_START_TIME, epochTime); + } else { + cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); + } +}); diff --git a/cypress/support/cypress-support/src/main.js b/cypress/support/cypress-support/src/main.js index 006ee084..7d2462d3 100644 --- a/cypress/support/cypress-support/src/main.js +++ b/cypress/support/cypress-support/src/main.js @@ -124,35 +124,9 @@ export default function (module) { beforeEach(() => { UTILS.getEnvVariable(CONSTANTS.FB_INTERACTIONLOGS).clearLogs(); - cy.getBeforeOperationObject().then(() => { - const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); - - UTILS.destroyGlobalObjects([CONSTANTS.LIFECYCLE_APP_OBJECT_LIST]); - - if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) === true) { - if (scenarioName) { - const requestMap = { - method: CONSTANTS.REQUEST_OVERRIDE_CALLS.CREATE_MARKER, - params: scenarioName, - }; - fireLog.info(`Firebolt Call to 1st party App: ${JSON.stringify(requestMap)} `); - cy.sendMessagetoPlatforms(requestMap).then((result) => { - fireLog.isTrue( - result.success, - 'Response for marker creation: ' + JSON.stringify(result) - ); - }); - - cy.startOrStopPerformanceService(CONSTANTS.INITIATED).then((response) => { - if (response) { - Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, true); - } - }); - } - } else { - cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); - } - }); + cy.getBeforeOperationObject(); + cy.initiatePerformanceMetrics(); + UTILS.destroyGlobalObjects([CONSTANTS.LIFECYCLE_APP_OBJECT_LIST]); }); /** @@ -261,17 +235,6 @@ export default function (module) { })(); }); - // after each - afterEach(() => { - if (UTILS.getEnvVariable(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, false) == true) { - cy.startOrStopPerformanceService(CONSTANTS.STOPPED).then((response) => { - if (response) { - Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, false); - } - }); - } - }); - /** * @module main * @function sendMessagetoPlatforms diff --git a/cypress/support/validations/performanceValidation.js b/cypress/support/validations/performanceValidation.js index 49ea6913..97c1498d 100644 --- a/cypress/support/validations/performanceValidation.js +++ b/cypress/support/validations/performanceValidation.js @@ -21,7 +21,10 @@ const UTILS = require('../cypress-support/src/utils'); Cypress.Commands.add('performanceValidation', (object) => { if (UTILS.getEnvVariable('performanceMetrics')) { - const { type, process, percentile, threshold } = object.validations[0]; + const type = object.validations?.[0]?.type || 'all'; + const process = object.validations?.[0]?.process || 'required'; + const percentile = object.validations?.[0]?.percentile; + const threshold = object.validations?.[0]?.threshold; const requestMap = { method: CONSTANTS.REQUEST_OVERRIDE_CALLS.PERFORMANCE_THRESHOLD_VALIDATOR, params: { type, process, percentile, threshold }, From 9aabd0ee1e0bfdf576acf6803e50593010bea790 Mon Sep 17 00:00:00 2001 From: "preethi.m" Date: Mon, 28 Oct 2024 15:30:35 +0530 Subject: [PATCH 4/5] Adding comments and descriptions --- cypress/fixtures/docs/validations.md | 1 + cypress/support/cypress-commands/commands.js | 11 +++++++++++ cypress/support/cypress-support/src/main.js | 11 +++++++++-- .../support/validations/performanceValidation.js | 14 +++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cypress/fixtures/docs/validations.md b/cypress/fixtures/docs/validations.md index 37dcfea3..c4656d6e 100644 --- a/cypress/fixtures/docs/validations.md +++ b/cypress/fixtures/docs/validations.md @@ -10,6 +10,7 @@ | undefined | Used when the incoming response has to be validated against undefined value. | | schemaOnly | When validation type is `schemaOnly`, it will skip content validation and stops at schema validation | | screenshotValidation | screenshotValidation is used to validate the screenshot captured by the device. | +| performanceValidation | Used to validate performance metrics against specified thresholds. | ## regEx diff --git a/cypress/support/cypress-commands/commands.js b/cypress/support/cypress-commands/commands.js index 6e9932f0..52456be1 100644 --- a/cypress/support/cypress-commands/commands.js +++ b/cypress/support/cypress-commands/commands.js @@ -1549,10 +1549,20 @@ Cypress.Commands.add('envConfigSetup', () => { fireLog.info('No additional config module environment setup'); }); +/** + * @module commands + * @function initiatePerformanceMetrics + * @description Creates a marker and saves the start time in THRESHOLD_MONITOR_START_TIME env, if performance metrics is enabled. + * @example + * cy.initiatePerformanceMetrics() + */ Cypress.Commands.add('initiatePerformanceMetrics', () => { + // Retrieve the scenario name from the env. const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); + // Check if performance metrics is enabled if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) === true) { + // Request to create a marker const requestMap = { method: CONSTANTS.REQUEST_OVERRIDE_CALLS.CREATE_MARKER, params: scenarioName, @@ -1562,6 +1572,7 @@ Cypress.Commands.add('initiatePerformanceMetrics', () => { fireLog.isTrue(result.success, 'Response for marker creation: ' + JSON.stringify(result)); }); + // Save the start time in the environment variable const epochTime = Number.parseInt(Date.now() / 1000); Cypress.env(CONSTANTS.THRESHOLD_MONITOR_START_TIME, epochTime); } else { diff --git a/cypress/support/cypress-support/src/main.js b/cypress/support/cypress-support/src/main.js index 7d2462d3..d55d24be 100644 --- a/cypress/support/cypress-support/src/main.js +++ b/cypress/support/cypress-support/src/main.js @@ -89,7 +89,15 @@ export default function (module) { Cypress.env(CONSTANTS.MESSAGE_QUEUE, messageQueue); UTILS.parseExceptionList(); cy.getModuleReqIdJson(); - + if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) == true) { + cy.startOrStopPerformanceService(CONSTANTS.INITIATED).then((response) => { + if (response) { + Cypress.env(CONSTANTS.IS_PERFORMANCE_METRICS_ENABLED, true); + } + }); + } else { + cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); + } // Merge fireboltCalls const v1FireboltCallsData = UTILS.getEnvVariable('fireboltCallsJson'); const v2FireboltCallsData = _.merge( @@ -123,7 +131,6 @@ export default function (module) { // beforeEach beforeEach(() => { UTILS.getEnvVariable(CONSTANTS.FB_INTERACTIONLOGS).clearLogs(); - cy.getBeforeOperationObject(); cy.initiatePerformanceMetrics(); UTILS.destroyGlobalObjects([CONSTANTS.LIFECYCLE_APP_OBJECT_LIST]); diff --git a/cypress/support/validations/performanceValidation.js b/cypress/support/validations/performanceValidation.js index 97c1498d..18b4c0cd 100644 --- a/cypress/support/validations/performanceValidation.js +++ b/cypress/support/validations/performanceValidation.js @@ -19,17 +19,29 @@ const CONSTANTS = require('../constants/constants'); const UTILS = require('../cypress-support/src/utils'); +/** + * @module performanceValidation + * @function performanceValidation + * @description Validates performance metrics based on provided validation parameters. + * @param {Object} object - The object to validate + * @example + * cy.performanceValidation({}) + */ Cypress.Commands.add('performanceValidation', (object) => { + // Check if performance metrics are enabled if (UTILS.getEnvVariable('performanceMetrics')) { + // Extract validation params or set defaults const type = object.validations?.[0]?.type || 'all'; const process = object.validations?.[0]?.process || 'required'; const percentile = object.validations?.[0]?.percentile; const threshold = object.validations?.[0]?.threshold; + // Call fetchPerformanceThreshold to validate performance metrics const requestMap = { method: CONSTANTS.REQUEST_OVERRIDE_CALLS.PERFORMANCE_THRESHOLD_VALIDATOR, params: { type, process, percentile, threshold }, }; + fireLog.info('Performance validation has started'); cy.sendMessagetoPlatforms(requestMap).then((result) => { if (result.error) { cy.log('Failed to fetch and validate the performance metrics').then(() => { @@ -44,6 +56,6 @@ Cypress.Commands.add('performanceValidation', (object) => { } }); } else { - fireLog.info('performanceMetrics are disabled.'); + fireLog.info('PerformanceMetrics are disabled.'); } }); From fb3a7fa18b4fad5415097323408b6f4df2307480 Mon Sep 17 00:00:00 2001 From: "preethi.m" Date: Fri, 1 Nov 2024 12:55:38 +0530 Subject: [PATCH 5/5] Addressing review comments --- cypress/support/constants/constants.js | 1 + cypress/support/cypress-commands/commands.js | 12 +++++------- cypress/support/step_definitions/testSetup.js | 9 +++++++++ cypress/support/validations/performanceValidation.js | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cypress/support/constants/constants.js b/cypress/support/constants/constants.js index 1d8669ec..fdec15ce 100644 --- a/cypress/support/constants/constants.js +++ b/cypress/support/constants/constants.js @@ -503,6 +503,7 @@ module.exports = { FIREBOLT_INTERACTION: 'FireboltInteraction', PENDING_FEATURES: 'pendingFeatures', PERFORMANCE_VALIDATION: 'performanceValidation', + MARKER_CREATION_STATUS: 'markerCreationStatus', }; function getSanityReportPath() { // Check if Cypress is defined, for cypress test context diff --git a/cypress/support/cypress-commands/commands.js b/cypress/support/cypress-commands/commands.js index 52456be1..5f0c2d16 100644 --- a/cypress/support/cypress-commands/commands.js +++ b/cypress/support/cypress-commands/commands.js @@ -1557,25 +1557,23 @@ Cypress.Commands.add('envConfigSetup', () => { * cy.initiatePerformanceMetrics() */ Cypress.Commands.add('initiatePerformanceMetrics', () => { - // Retrieve the scenario name from the env. - const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); - // Check if performance metrics is enabled if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS) === true) { + // Retrieve the scenario name from the env. + const scenarioName = Cypress.env(CONSTANTS.SCENARIO_NAME); + // Request to create a marker const requestMap = { method: CONSTANTS.REQUEST_OVERRIDE_CALLS.CREATE_MARKER, params: scenarioName, }; - fireLog.info(`Firebolt Call to 1st party App: ${JSON.stringify(requestMap)} `); cy.sendMessagetoPlatforms(requestMap).then((result) => { - fireLog.isTrue(result.success, 'Response for marker creation: ' + JSON.stringify(result)); + const markerCreated = result.success; + Cypress.env(CONSTANTS.MARKER_CREATION_STATUS, markerCreated); }); // Save the start time in the environment variable const epochTime = Number.parseInt(Date.now() / 1000); Cypress.env(CONSTANTS.THRESHOLD_MONITOR_START_TIME, epochTime); - } else { - cy.log(CONSTANTS.PERFORMANCE_METRICS_NOT_ACTIVE); } }); diff --git a/cypress/support/step_definitions/testSetup.js b/cypress/support/step_definitions/testSetup.js index 282fd48b..216f288e 100644 --- a/cypress/support/step_definitions/testSetup.js +++ b/cypress/support/step_definitions/testSetup.js @@ -73,6 +73,15 @@ Given('the environment has been set up for {string} tests', (test) => { } } } + // Check the marker creation status + if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS)) { + const markerCreated = Cypress.env(CONSTANTS.MARKER_CREATION_STATUS); + if (markerCreated) { + fireLog.info('Marker has been created successfully'); + } else { + fireLog.fail('Marker creation failed'); + } + } // Calling the envConfigSetup command to setup the environment for the test from the config module. cy.envConfigSetup(); }); diff --git a/cypress/support/validations/performanceValidation.js b/cypress/support/validations/performanceValidation.js index 18b4c0cd..60f64b4b 100644 --- a/cypress/support/validations/performanceValidation.js +++ b/cypress/support/validations/performanceValidation.js @@ -29,7 +29,7 @@ const UTILS = require('../cypress-support/src/utils'); */ Cypress.Commands.add('performanceValidation', (object) => { // Check if performance metrics are enabled - if (UTILS.getEnvVariable('performanceMetrics')) { + if (UTILS.getEnvVariable(CONSTANTS.PERFORMANCE_METRICS)) { // Extract validation params or set defaults const type = object.validations?.[0]?.type || 'all'; const process = object.validations?.[0]?.process || 'required';