From 1102102e8f9ce854aaa43cdccbb393069033a0e4 Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Thu, 30 May 2024 17:10:47 +0200 Subject: [PATCH] [Cypress] Wait for VE modules to load This should make sure events are added to the corresponding UI elements and reduce flaky test results even more. Change-Id: I2317321912945ef20f8f49405b54fddd65c618d1 --- .../cypress/e2e/tests/ve-cite/reuseRefs.cy.js | 12 ++++---- .../cypress/e2e/tests/ve-cite/templates.cy.js | 10 +++---- .../e2e/tests/ve-cite/veCiteIntegration.cy.js | 9 +++--- tests/cypress/e2e/utils/functions.helper.js | 29 ++++++++++++++++++- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/tests/cypress/e2e/tests/ve-cite/reuseRefs.cy.js b/tests/cypress/e2e/tests/ve-cite/reuseRefs.cy.js index c6493166d..a2102c094 100644 --- a/tests/cypress/e2e/tests/ve-cite/reuseRefs.cy.js +++ b/tests/cypress/e2e/tests/ve-cite/reuseRefs.cy.js @@ -8,7 +8,7 @@ const wikiText = `This is reference #1: ${ refText1 }
` `This is reference #3 ${ refText2 }
` + ''; -let citoidLoaded; +let usesCitoid; describe( 'Re-using refs in Visual Editor', () => { beforeEach( () => { @@ -19,7 +19,7 @@ describe( 'Re-using refs in Visual Editor', () => { helpers.editPage( title, wikiText ); cy.window().then( async ( win ) => { - citoidLoaded = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); + usesCitoid = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); win.localStorage.setItem( 've-beta-welcome-dialog', 1 ); win.localStorage.setItem( 've-hideusered', 1 ); } ); @@ -34,9 +34,7 @@ describe( 'Re-using refs in Visual Editor', () => { } ); - helpers.visitTitle( title, { veaction: 'edit' } ); - - helpers.waitForVEToLoad(); + helpers.openVEForEditingReferences( title, usesCitoid ); } ); it( 'should display re-used reference in article with correct footnote number and notification in context dialog', () => { @@ -46,7 +44,7 @@ describe( 'Re-using refs in Visual Editor', () => { // Place cursor next to ref #2 in order to add re-use ref next to it cy.contains( '.mw-reflink-text', '[2]' ).type( '{rightarrow}' ); - if ( citoidLoaded ) { + if ( usesCitoid ) { helpers.openVECiteoidReuseDialog(); } else { helpers.openVECiteReuseDialog(); @@ -87,7 +85,7 @@ describe( 'Re-using refs in Visual Editor', () => { // Place cursor next to ref #1 in order to add re-used ref next to it cy.contains( '.mw-reflink-text', '[1]' ).first().type( '{rightarrow}' ); - if ( citoidLoaded ) { + if ( usesCitoid ) { helpers.openVECiteoidReuseDialog(); } else { helpers.openVECiteReuseDialog(); diff --git a/tests/cypress/e2e/tests/ve-cite/templates.cy.js b/tests/cypress/e2e/tests/ve-cite/templates.cy.js index f7ebfc515..52f058fc4 100644 --- a/tests/cypress/e2e/tests/ve-cite/templates.cy.js +++ b/tests/cypress/e2e/tests/ve-cite/templates.cy.js @@ -11,7 +11,7 @@ const refText1 = 'This is citation #1 for reference #1'; const wikiText = `${ wikiText1 } ${ refText1 }
` + ''; -let citoidLoaded; +let usesCitoid; describe( 'Re-using refs in Visual Editor using templates', () => { @@ -65,20 +65,18 @@ describe( 'Re-using refs in Visual Editor using templates', () => { await win.mw.loader.using( 'mediawiki.base' ).then( async function () { await win.mw.hook( 'wikipage.content' ).add( function () { } ); } ); - citoidLoaded = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); + usesCitoid = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); win.localStorage.setItem( 've-beta-welcome-dialog', 1 ); win.localStorage.setItem( 've-hideusered', 1 ); } ); - // Open VE edit mode - helpers.visitTitle( title, { veaction: 'edit' } ); - helpers.waitForVEToLoad(); + helpers.openVEForEditingReferences( title, usesCitoid ); } ); it( 'should add a template reference and verify correct content in both saved and edit mode', () => { cy.contains( '.mw-reflink-text', '[1]' ).type( '{rightarrow}' ); - if ( citoidLoaded ) { + if ( usesCitoid ) { cy.get( '.ve-ui-toolbar-group-citoid' ).click(); // Switch to Manual tab diff --git a/tests/cypress/e2e/tests/ve-cite/veCiteIntegration.cy.js b/tests/cypress/e2e/tests/ve-cite/veCiteIntegration.cy.js index d0e2086e7..db4d7a36f 100644 --- a/tests/cypress/e2e/tests/ve-cite/veCiteIntegration.cy.js +++ b/tests/cypress/e2e/tests/ve-cite/veCiteIntegration.cy.js @@ -9,7 +9,7 @@ const wikiText = `This is reference #1: ${ refText1 }
` `This is reference #3 ${ refText2 }
` + ''; -let citoidLoaded; +let usesCitoid; describe( 'Visual Editor Cite Integration', () => { before( () => { @@ -22,13 +22,12 @@ describe( 'Visual Editor Cite Integration', () => { helpers.waitForMWLoader(); cy.window().then( async ( win ) => { - citoidLoaded = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); + usesCitoid = win.mw.loader.getModuleNames().includes( 'ext.citoid.visualEditor' ); win.localStorage.setItem( 've-beta-welcome-dialog', 1 ); win.localStorage.setItem( 've-hideusered', 1 ); } ); - helpers.visitTitle( title, { veaction: 'edit' } ); - helpers.waitForVEToLoad(); + helpers.openVEForEditingReferences( title, usesCitoid ); } ); it( 'should edit and verify reference content in Visual Editor', () => { @@ -49,7 +48,7 @@ describe( 'Visual Editor Cite Integration', () => { } ); it( 'should display existing references in the Cite re-use dialog', () => { - if ( citoidLoaded ) { + if ( usesCitoid ) { helpers.openVECiteoidReuseDialog(); } else { diff --git a/tests/cypress/e2e/utils/functions.helper.js b/tests/cypress/e2e/utils/functions.helper.js index c251ccaa7..c5395d14b 100644 --- a/tests/cypress/e2e/utils/functions.helper.js +++ b/tests/cypress/e2e/utils/functions.helper.js @@ -1,8 +1,35 @@ import querystring from 'querystring'; -export function waitForVEToLoad() { +export function openVEForEditingReferences( title, usesCitoid ) { + visitTitle( title, { veaction: 'edit' } ); + waitForVECiteToLoad(); + if ( usesCitoid ) { + waitForVECitoidToLoad(); + } +} + +export function waitForVECiteToLoad() { cy.get( '.ve-init-mw-desktopArticleTarget-toolbar-open', { timeout: 7000 } ) .should( 'be.visible' ); + cy.window() + .should( 'have.property', 'mw' ) + .and( 'have.property', 'loader' ) + .and( 'have.property', 'getState' ); + cy.window() + .should( + ( win ) => win.mw.loader.getState( 'ext.cite.visualEditor' ) === 'ready' + ); +} + +export function waitForVECitoidToLoad() { + cy.window() + .should( 'have.property', 'mw' ) + .and( 'have.property', 'loader' ) + .and( 'have.property', 'getState' ); + cy.window() + .should( + ( win ) => win.mw.loader.getState( 'ext.citoid.visualEditor' ) === 'ready' + ); } function clickUntilVisible( clickElement, expectedSelector, timeout = 5000 ) {