From c1f45555eb56eab1bcf099926ce53452ff14d01d Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 14 Jun 2022 13:26:53 -0300 Subject: [PATCH 1/4] Speed up before hook --- tests/cypress/support/commands.js | 32 +--------------- tests/cypress/support/global-hooks.js | 54 +++++++++++---------------- tests/cypress/support/index.js | 33 ++++++++++++++++ 3 files changed, 55 insertions(+), 64 deletions(-) diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index 1983e0204d..233db50237 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -158,37 +158,7 @@ Cypress.Commands.add('publishPost', (postData) => { }); Cypress.Commands.add('updateFeatures', (newFeaturesValues = {}) => { - const features = { - search: { - active: 1, - highlight_enabled: true, - highlight_excerpt: true, - highlight_tag: 'mark', - highlight_color: '#157d84', - }, - related_posts: { - active: 1, - }, - facets: { - active: 1, - }, - searchordering: { - active: 1, - }, - autosuggest: { - active: 1, - }, - woocommerce: { - active: 0, - }, - protected_content: { - active: 0, - }, - users: { - active: 0, - }, - ...newFeaturesValues, - }; + const features = Object.assign({}, cy.elasticPress.defaultFeatures, ...newFeaturesValues); const escapedFeatures = JSON.stringify(features); diff --git a/tests/cypress/support/global-hooks.js b/tests/cypress/support/global-hooks.js index f46b981735..f52f467b95 100644 --- a/tests/cypress/support/global-hooks.js +++ b/tests/cypress/support/global-hooks.js @@ -1,41 +1,29 @@ window.indexNames = null; -let setFeatures = false; - before(() => { - // Clear sync from previous tests. - cy.wpCli('wp elasticpress clear-index'); + cy.wpCliEval( + ` + // Clear any stuck sync process. + \\ElasticPress\\IndexHelper::factory()->clear_index_meta(); - if (!window.indexNames) { - cy.wpCli('wp elasticpress get-indexes').then((wpCliResponse) => { - window.indexNames = JSON.parse(wpCliResponse.stdout); - }); - } + $features = json_decode( '${JSON.stringify(cy.elasticPress.defaultFeatures)}', true ); + + if ( ! \\ElasticPress\\Utils\\is_epio() ) { + $host = \\ElasticPress\\Utils\\get_host(); + $host = str_replace( '172.17.0.1', 'localhost', $host ); + $index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name(); + $as_endpoint_url = $host . $index_name . '/_search'; + + $features['autosuggest']['endpoint_url'] = $as_endpoint_url; + } - if (!setFeatures) { - cy.wpCliEval( - `if ( \\ElasticPress\\Utils\\is_epio() ) { - exit; - } - $host = \\ElasticPress\\Utils\\get_host(); - $host = str_replace( '172.17.0.1', 'localhost', $host ); - $index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name(); - echo $host . $index_name . '/_search'; - `, - ).then((searchEndpointUrl) => { - if (searchEndpointUrl.stdout === '') { - cy.updateFeatures(); - } else { - cy.updateFeatures({ - autosuggest: { - active: 1, - endpoint_url: searchEndpointUrl.stdout, - }, - }); - } - }); - setFeatures = true; - } + update_option( 'ep_feature_settings', $features ); + + WP_CLI::runcommand('elasticpress get-indexes'); + `, + ).then((wpCliResponse) => { + window.indexNames = JSON.parse(wpCliResponse.stdout); + }); }); afterEach(() => { diff --git a/tests/cypress/support/index.js b/tests/cypress/support/index.js index c041045886..f42dec1025 100644 --- a/tests/cypress/support/index.js +++ b/tests/cypress/support/index.js @@ -19,3 +19,36 @@ import './global-hooks'; // Alternatively you can use CommonJS syntax: // require('./commands') + +cy.elasticPress = { + defaultFeatures: { + search: { + active: 1, + highlight_enabled: true, + highlight_excerpt: true, + highlight_tag: 'mark', + highlight_color: '#157d84', + }, + related_posts: { + active: 1, + }, + facets: { + active: 1, + }, + searchordering: { + active: 1, + }, + autosuggest: { + active: 1, + }, + woocommerce: { + active: 0, + }, + protected_content: { + active: 0, + }, + users: { + active: 0, + }, + }, +}; From 16e8b6410a0a85afdbb1ce7e6f20ef4212c38a15 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 14 Jun 2022 13:27:10 -0300 Subject: [PATCH 2/4] Delete all indices --- tests/cypress/integration/dashboard-sync.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cypress/integration/dashboard-sync.spec.js b/tests/cypress/integration/dashboard-sync.spec.js index e00dbf3c53..f946764a65 100644 --- a/tests/cypress/integration/dashboard-sync.spec.js +++ b/tests/cypress/integration/dashboard-sync.spec.js @@ -76,7 +76,7 @@ describe('Dashboard Sync', () => { // Sync and remove, so EP doesn't think it is a fresh install. cy.wpCli('wp elasticpress index --setup --yes'); - cy.wpCli('wp elasticpress delete-index --yes'); + cy.wpCli('wp elasticpress delete-index --yes --network-wide'); cy.visitAdminPage('network/admin.php?page=elasticpress-health'); cy.get('.wrap').should( From 7d5c5bb73fcc0d934bb3a668caacfa20a466b363 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 14 Jun 2022 13:27:45 -0300 Subject: [PATCH 3/4] adjust organization and retry-ability --- .../integration/features/facets.spec.js | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/tests/cypress/integration/features/facets.spec.js b/tests/cypress/integration/features/facets.spec.js index d934bb9ccb..0608bdd8e7 100644 --- a/tests/cypress/integration/features/facets.spec.js +++ b/tests/cypress/integration/features/facets.spec.js @@ -6,37 +6,43 @@ describe('Facets Feature', () => { * @param {string} category The category slug. */ function createWidget(title, category) { - cy.intercept('/wp-json/wp/v2/widget-types/*/encode*').as('legacyWidgets'); cy.openWidgetsPage(); - cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); - cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]').click({ - force: true, + let createdWidgetsLength = 0; + + cy.get('.is-opened').then(($openedWidgetArea) => { + createdWidgetsLength = $openedWidgetArea.find('.wp-block-legacy-widget').length; + + cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); + cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]').click( + { + force: true, + }, + ); + + cy.get( + `.is-opened .wp-block-legacy-widget:eq(${createdWidgetsLength}):visible .widefat:visible`, + ) + .closest('.widget-ep-facet') + .within(() => { + cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( + title, + true, + ); + cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(category); + }); + + /** + * Wait for WordPress to recognize the title typed. + * + * @todo investigate why this is needed. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); + cy.get('body').should('contain.text', 'Widgets saved.'); }); - cy.wait('@legacyWidgets'); - // eslint-disable-next-line cypress/no-unnecessary-waiting -- JS processing - cy.wait(1000); - - cy.get('.is-opened .widget-ep-facet') - .last() - .within(() => { - cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( - title, - true, - ); - cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(category); - }); - - /** - * Wait for WordPress to recognize the title typed. - * - * @todo investigate why this is needed. - */ - // eslint-disable-next-line cypress/no-unnecessary-waiting - cy.wait(2000); - - cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); - cy.get('body').should('contain.text', 'Widgets saved.'); } before(() => { @@ -44,20 +50,17 @@ describe('Facets Feature', () => { cy.wpCli('widget reset --all'); cy.wpCli('elasticpress index --setup --yes'); - - // Initial widget that will be used for all tests. - createWidget('Facet (categories)', 'category'); }); it('Can see the widget in the frontend', () => { + createWidget('Facet (categories)', 'category'); + cy.visit('/'); - // Check if the widget is visible. + // Check if the first widget is visible. cy.get('.widget_ep-facet').should('be.visible'); cy.contains('.widget-title', 'Facet (categories)').should('be.visible'); - }); - it('Can use widgets', () => { // Create a second widget, so we can test both working together. createWidget('Facet (Tags)', 'post_tag'); From 4cd00b6e2ebcc22a12555bf2bab84591a3b1a3bf Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 14 Jun 2022 14:37:00 -0300 Subject: [PATCH 4/4] adjust async calls --- .../integration/features/facets.spec.js | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/tests/cypress/integration/features/facets.spec.js b/tests/cypress/integration/features/facets.spec.js index 0608bdd8e7..77035816c1 100644 --- a/tests/cypress/integration/features/facets.spec.js +++ b/tests/cypress/integration/features/facets.spec.js @@ -14,45 +14,49 @@ describe('Facets Feature', () => { createdWidgetsLength = $openedWidgetArea.find('.wp-block-legacy-widget').length; cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); - cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]').click( - { + cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]') + .click({ force: true, - }, - ); - - cy.get( - `.is-opened .wp-block-legacy-widget:eq(${createdWidgetsLength}):visible .widefat:visible`, - ) - .closest('.widget-ep-facet') - .within(() => { - cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( - title, - true, - ); - cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(category); + }) + .then(() => { + cy.get( + `.is-opened .wp-block-legacy-widget:eq(${createdWidgetsLength}) .wp-block-legacy-widget__edit-form:visible .widefat:visible`, + { timeout: 10000 }, + ) + .closest('.widget-ep-facet') + .within(() => { + cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType( + title, + true, + ); + cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select( + category, + ); + }); + + /** + * Wait for WordPress to recognize the title typed. + * + * @todo investigate why this is needed. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); + cy.get('body').should('contain.text', 'Widgets saved.'); }); - - /** - * Wait for WordPress to recognize the title typed. - * - * @todo investigate why this is needed. - */ - // eslint-disable-next-line cypress/no-unnecessary-waiting - cy.wait(2000); - - cy.get('.edit-widgets-header__actions .components-button.is-primary').click(); - cy.get('body').should('contain.text', 'Widgets saved.'); }); } before(() => { cy.maybeEnableFeature('facets'); - cy.wpCli('widget reset --all'); cy.wpCli('elasticpress index --setup --yes'); }); it('Can see the widget in the frontend', () => { + cy.wpCli('widget reset --all'); + createWidget('Facet (categories)', 'category'); cy.visit('/');