From 13e5e49a41281f656041bfb294bb45b366f89c1c Mon Sep 17 00:00:00 2001 From: Kaituo Li Date: Mon, 26 Aug 2024 14:07:12 -0700 Subject: [PATCH] Fix Cypress Test Flakiness by Executing cy.visit Before cy.request This PR addresses intermittent failures in the AD Cypress tests related to a known issue in Cypress (https://github.com/cypress-io/cypress/issues/25397). The specific failure observed was: ``` Sample detector "before all" hook for "Empty message with modal": AssertionError: Timed out retrying after 60000ms: Expected to find element: `[data-test-subj="viewSampleDetectorLink"]`, but never found it. ``` To mitigate this issue, we applied a workaround suggested in the GitHub issue, ensuring that cy.visit is executed before cy.request. This change helps stabilize the tests by properly loading AD overview page before making any API requests. Testing: * Ran all Cypress tests with the modified code to verify the fix. Signed-off-by: Kaituo Li --- .../create_detector_spec.js | 1 + .../detector_configuration_spec.js | 1 + .../historical_analysis_spec.js | 4 +++- .../anomaly-detection-dashboards-plugin/overview_spec.js | 2 +- .../sample_detector_spec.js | 4 ++++ .../vis_augmenter/associate_detector_spec.js | 2 ++ .../vis_augmenter/augment_vis_saved_object_spec.js | 2 ++ .../vis_augmenter/view_anomaly_events_spec.js | 2 ++ 8 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/create_detector_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/create_detector_spec.js index 556e0b981..a1829941a 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/create_detector_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/create_detector_spec.js @@ -15,6 +15,7 @@ context('Create detector workflow', () => { // Index some sample data first beforeEach(() => { + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); cy.deleteAllIndices(); cy.deleteADSystemIndices(); cy.fixture(AD_FIXTURE_BASE_PATH + 'sample_test_data.txt').then((data) => { diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/detector_configuration_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/detector_configuration_spec.js index 66b4f97ac..c4a626e9d 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/detector_configuration_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/detector_configuration_spec.js @@ -9,6 +9,7 @@ context('Detector configuration page', () => { // Creating a sample detector and visiting the config page. Stopping the detector // for easier checks when editing detector before(() => { + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); cy.deleteAllIndices(); cy.deleteADSystemIndices(); cy.visit(AD_URL.OVERVIEW); diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/historical_analysis_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/historical_analysis_spec.js index 1cf93e8c9..929814808 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/historical_analysis_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/historical_analysis_spec.js @@ -52,10 +52,12 @@ describe('Historical results page', () => { // Creating a sample detector and visiting the config page before(() => { + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); cy.deleteAllIndices(); cy.deleteADSystemIndices(); cy.wait(5000); - cy.visit(AD_URL.OVERVIEW); + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); + cy.wait(2000); cy.get('[data-test-subj=createHttpSampleDetectorButton]').then(() => { cy.getElementByTestId('createHttpSampleDetectorButton').click(); cy.wait(10000); diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/overview_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/overview_spec.js index c8e7acb9c..26b1cdb46 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/overview_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/overview_spec.js @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { AD_URL } from '../../../utils/constants'; +import { AD_URL } from '../../../utils/plugins/anomaly-detection-dashboards-plugin/constants'; context('Overview page', () => { function validatePageElements() { diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/sample_detector_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/sample_detector_spec.js index ebcab7e29..8fcb07f89 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/sample_detector_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/sample_detector_spec.js @@ -4,8 +4,12 @@ */ import { createSampleDetector } from '../../../utils/helpers'; +import { AD_URL } from '../../../utils/plugins/anomaly-detection-dashboards-plugin/constants'; context('Sample detectors', () => { + before(() => { + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); + }); beforeEach(() => { cy.deleteAllIndices(); cy.deleteADSystemIndices(); diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/associate_detector_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/associate_detector_spec.js index 042546d83..e345b7d53 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/associate_detector_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/associate_detector_spec.js @@ -20,6 +20,7 @@ import { INDEX_SETTINGS_FILEPATH_SIMPLE, SAMPLE_DATA_FILEPATH_SIMPLE, } from '../../../../utils/constants'; +import { AD_URL } from '../../../../utils/plugins/anomaly-detection-dashboards-plugin/constants'; describe('Anomaly detection integration with vis augmenter', () => { const indexName = 'ad-vis-augmenter-sample-index'; @@ -51,6 +52,7 @@ describe('Anomaly detection integration with vis augmenter', () => { dashboardName, [visualizationSpec] ); + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); }); after(() => { diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/augment_vis_saved_object_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/augment_vis_saved_object_spec.js index 1f02b0f90..76827c712 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/augment_vis_saved_object_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/augment_vis_saved_object_spec.js @@ -18,6 +18,7 @@ import { INDEX_SETTINGS_FILEPATH_SIMPLE, SAMPLE_DATA_FILEPATH_SIMPLE, } from '../../../../utils/constants'; +import { AD_URL } from '../../../../utils/plugins/anomaly-detection-dashboards-plugin/constants'; describe('AD augment-vis saved objects', () => { const commonUI = new CommonUI(cy); @@ -50,6 +51,7 @@ describe('AD augment-vis saved objects', () => { dashboardName, [visualizationSpec] ); + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); }); after(() => { diff --git a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/view_anomaly_events_spec.js b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/view_anomaly_events_spec.js index 40583a39f..b195edeaf 100644 --- a/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/view_anomaly_events_spec.js +++ b/cypress/integration/plugins/anomaly-detection-dashboards-plugin/vis_augmenter/view_anomaly_events_spec.js @@ -17,6 +17,7 @@ import { INDEX_SETTINGS_FILEPATH_SIMPLE, SAMPLE_DATA_FILEPATH_SIMPLE, } from '../../../../utils/constants'; +import { AD_URL } from '../../../../utils/plugins/anomaly-detection-dashboards-plugin/constants'; describe('View anomaly events in flyout', () => { const indexName = 'ad-vis-augmenter-sample-index'; @@ -48,6 +49,7 @@ describe('View anomaly events in flyout', () => { dashboardName, [visualizationSpec] ); + cy.visit(AD_URL.OVERVIEW, { timeout: 10000 }); }); after(() => {