From 450047931082167ee29246c21266359b2716e8fa Mon Sep 17 00:00:00 2001 From: Rebecca Hum Date: Wed, 20 Nov 2024 12:04:07 -0700 Subject: [PATCH] Search e2e: Fix tests to make 6.7 compatible --- .../features/custom-results.spec.js | 106 ++++++----- tests/search/e2e/support/commands.js | 34 +++- .../e2e/support/commands/block-editor.js | 167 +++++++++++++++++- 3 files changed, 248 insertions(+), 59 deletions(-) diff --git a/tests/search/e2e/integration/features/custom-results.spec.js b/tests/search/e2e/integration/features/custom-results.spec.js index 9932455c24..20a8775a7f 100644 --- a/tests/search/e2e/integration/features/custom-results.spec.js +++ b/tests/search/e2e/integration/features/custom-results.spec.js @@ -2,20 +2,30 @@ describe('Custom Results', () => { const testPost = 'test-post'; before(() => { - cy.wpCli("wp post list --post_type='ep-pointer' --format=ids", true).then( - (wpCliResponse) => { - if (wpCliResponse.code === 0) { - cy.wpCli(`wp post delete ${wpCliResponse.stdout} --force`, true); - } - }, - ); - - cy.wpCli(`wp post list --s='${testPost}' --ep_integrate='false' --format=ids`, true).then( - (wpCliResponse) => { - if (wpCliResponse.code === 0) { - cy.wpCli(`wp post delete ${wpCliResponse.stdout} --force`, true); - } - }, + cy.wpCliEval( + ` + $ep_pointers = get_posts( + [ + 'post_type' => 'ep-pointer', + 'numberposts' => 999, + ] + ); + foreach( $ep_pointers as $pointer ) { + wp_delete_post( $pointer->ID, true ); + } + + $posts = new \\WP_Query( + [ + 's' => '${testPost}', + 'ep_integrate' => false, + 'fields' => 'ids', + 'numberposts' => 999, + ] + ); + foreach( $posts->posts as $post ) { + wp_delete_post( $post->ID, true ); + } + ` ); }); @@ -25,18 +35,20 @@ describe('Custom Results', () => { cy.login(); cy.visitAdminPage('post-new.php?post_type=ep-pointer'); - cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest'); + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as( + 'ajaxRequest' + ); cy.get('#titlewrap input').type(searchTerm); cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200); // change the position of the post - // eslint-disable-next-line jest/valid-expect-in-promise cy.dragAndDrop( '.pointers .pointer:first-of-type .dashicons-menu', - '.pointers .pointer:last-of-type .dashicons-menu', + '.pointers .pointer:last-of-type .dashicons-menu' ).then(() => { // save the posts positions in a list + // eslint-disable-next-line cypress/unsafe-to-chain-command cy.get('.pointers .pointer .title') .each((post) => { searchResult.push(post[0].innerText); @@ -48,11 +60,11 @@ describe('Custom Results', () => { cy.visit(`?s=${searchTerm}`); // verify the result of the search is in the same position. - cy.get('h2.entry-title').each( - (post, index) => { - expect(post[0].innerText).to.equal(searchResult[index]); - }, - ); + cy.get( + `article:nth-child(-n+${searchResult.length}) .entry-title` + ).each((post, index) => { + expect(post[0].innerText).to.equal(searchResult[index]); + }); }); }); }); @@ -66,12 +78,16 @@ describe('Custom Results', () => { }); cy.visitAdminPage('post-new.php?post_type=ep-pointer'); - cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest'); + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as( + 'ajaxRequest' + ); cy.get('#titlewrap input').type(searchTerm); cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200); - cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_search*').as('ajaxRequest'); + cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_search*').as( + 'ajaxRequest' + ); // search for the post. cy.get('.search-pointers').type(testPost); @@ -81,35 +97,31 @@ describe('Custom Results', () => { cy.get('.pointer-result:first-of-type .dashicons-plus.add-pointer').click(); // save the posts positions in a list - // eslint-disable-next-line jest/valid-expect-in-promise + // eslint-disable-next-line cypress/unsafe-to-chain-command cy.get('.pointers .pointer:nth-child(-n+5) .title') // 5 being the number of posts per page, as we will check only the first page. .each((post) => { searchResult.push(post[0].innerText); }) .then(() => { expect(searchResult.length).to.be.gt(0); - cy.get('#publish') - .click() - .then(() => { - /** - * Give Elasticsearch some time to update the posts in custom results. - */ - // eslint-disable-next-line cypress/no-unnecessary-waiting - cy.wait(4000); - - // VIP: Since cron doesn't run, we need to manually force a sync and clear cache since the query will be cached. - cy.get('#publish').click(); - cy.wpCli(`wp cache flush`, true); - - cy.visit(`?s=${searchTerm}`); - - // verify the result of the search is in the same position. - cy.get('h2.entry-title').each( - (post, index) => { - expect(post[0].innerText).to.equal(searchResult[index]); - }, - ); - }); + cy.get('#publish').click(); + /** + * Give Elasticsearch some time to update the posts in custom results. + */ + cy.wait(4000); + + // VIP: Since cron doesn't run, we need to manually force a sync and clear cache since the query will be cached. + cy.get('#publish').click(); + cy.wpCli(`wp cache flush`, true); + + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(1000); + cy.visit(`?s=${searchTerm}`); + + // verify the result of the search is in the same position. + cy.get('h2.entry-title').each((post, index) => { + expect(post[0].innerText).to.equal(searchResult[index]); + }); }); }); }); diff --git a/tests/search/e2e/support/commands.js b/tests/search/e2e/support/commands.js index ae7bbaa1e2..6a1f0a2db8 100644 --- a/tests/search/e2e/support/commands.js +++ b/tests/search/e2e/support/commands.js @@ -126,7 +126,6 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => { const welcomeGuide = $body.find( '.edit-post-welcome-guide .components-modal__header button', ); - cy.log(welcomeGuide); if (welcomeGuide.length) { welcomeGuide.click(); } @@ -135,16 +134,45 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => { cy.get('h1.editor-post-title__input, #post-title-0').clearThenType(newPostData.title); cy.get('.block-editor-default-block-appender__content').type(newPostData.content); + if (newPostData.password && newPostData.password !== '') { + cy.get('h1.editor-post-title__input').click(); + if (wpVersion === '6.0') { + cy.get('body').then(($body) => { + const $button = $body.find('.edit-post-post-visibility__toggle'); + if (!$button.is(':visible')) { + cy.get('.edit-post-header__settings button[aria-label="Settings"]').click(); + } + }); + cy.get('.edit-post-post-visibility__toggle').click(); + cy.get('.editor-post-visibility__dialog-radio, .editor-post-visibility__radio').check( + 'password', + ); + cy.get( + '.editor-post-visibility__dialog-password-input, .editor-post-visibility__password-input', + ).type(newPostData.password); + } else { + cy.get('body').then(($body) => { + const $button = $body.find('.components-dropdown.editor-post-status'); + if (!$button.is(':visible')) { + cy.get('.editor-header__settings button[aria-label="Settings"]').click(); + } + }); + cy.get('.components-dropdown.editor-post-status').click(); + cy.get('.editor-change-status__password-fieldset input[type="checkbox"]').click(); + cy.get('.editor-change-status__password-input input').type(newPostData.password); + } + } + if (newPostData.status && newPostData.status === 'draft') { cy.get('.editor-post-save-draft').click(); cy.get('.editor-post-saved-state').should('have.text', 'Saved'); } else { cy.get('.editor-post-publish-panel__toggle').should('be.enabled'); - cy.get('.editor-post-publish-panel__toggle').click(); + cy.get('.editor-post-publish-panel__toggle').click( { force: true } ); cy.get('.editor-post-publish-button').click(); - cy.get('.components-snackbar', { timeout: 6000 } ).should('be.visible'); + cy.get('.components-snackbar').should('be.visible'); if (viewPost) { cy.get('.post-publish-panel__postpublish-buttons a').contains('View Post').click(); diff --git a/tests/search/e2e/support/commands/block-editor.js b/tests/search/e2e/support/commands/block-editor.js index daee9ef017..d68d7c32f7 100644 --- a/tests/search/e2e/support/commands/block-editor.js +++ b/tests/search/e2e/support/commands/block-editor.js @@ -1,15 +1,24 @@ +/* global wpVersion */ + Cypress.Commands.add('openBlockSettingsSidebar', () => { cy.get('body').then(($el) => { if ($el.hasClass('widgets-php')) { cy.get('.edit-widgets-header__actions button[aria-label="Settings"]').click(); - cy.get('.edit-widgets-sidebar__panel-tab').contains('Block').click(); + cy.get('.edit-widgets-sidebar__panel-tab,.edit-widgets-sidebar__panel-tabs button') + .contains('Block') + .click(); } else { - cy.get('.editor-header__settings button[aria-label="Settings"]').then(($btn) => { - if (!$btn.hasClass('is-pressed')) { - $btn.click(); - } - cy.get('.editor-sidebar__panel-tabs').contains('Block').click(); - }); + cy.get( + `.edit-post-header__settings button[aria-label="Settings"], + .editor-header__settings button[aria-label="Settings"]`, + ).click(); + cy.get( + `.edit-post-sidebar__panel-tab, + .edit-post-sidebar__panel-tabs button, + .editor-sidebar__panel-tabs button`, + ) + .contains('Block') + .click(); } }); }); @@ -20,7 +29,13 @@ Cypress.Commands.add('openBlockInserter', () => { if ($body.find('.edit-widgets-layout__inserter-panel-content').length > 0) { return; } - cy.get('button[aria-label="Toggle block inserter"]').click(); + if ($body.hasClass('widgets-php')) { + cy.get('.edit-widgets-header-toolbar__inserter-toggle').click(); + } else { + cy.get( + '.edit-post-header-toolbar__inserter-toggle,.editor-document-tools__inserter-toggle', + ).click(); + } }); }); @@ -29,5 +44,139 @@ Cypress.Commands.add('getBlocksList', () => { }); Cypress.Commands.add('insertBlock', (blockName) => { - cy.get('.block-editor-block-types-list__item').contains(blockName).click(); + cy.get('.block-editor-inserter__search input[type="search"').clearThenType(blockName); + cy.get('.block-editor-block-types-list__item').contains(blockName).click({ force: true }); +}); + +Cypress.Commands.add('supportsBlockColors', { prevSubject: true }, (subject, isEdit) => { + if (isEdit) { + cy.get('.block-editor-block-inspector').as('blockInspector'); + + if (wpVersion === '6.0') { + cy.get( + '.color-block-support-panel .components-button[aria-label="View and add options"]', + ).click(); + + cy.get('.components-button[aria-label="Show Background"]').click(); + cy.get('.block-editor-panel-color-gradient-settings__dropdown').click(); + + cy.get('.components-button[aria-label="Color: Black"]').click(); + + cy.get('.color-block-support-panel').click(); + } else { + cy.get('.block-editor-block-inspector button[aria-label="Styles"]').click(); + cy.get('.block-editor-block-inspector button').contains('Background').click(); + + cy.get( + `.popover-slot button[aria-label="Color: Black"], + .block-editor-color-gradient-control__panel button[aria-label="Color: Black"]`, + ).click(); + + cy.get('.block-editor-block-inspector button[aria-label="Settings"]').click(); + } + } + + cy.wrap(subject).should('have.css', 'background-color', 'rgb(0, 0, 0)'); +}); + +Cypress.Commands.add('supportsBlockTypography', { prevSubject: true }, (subject, isEdit) => { + if (isEdit) { + if (wpVersion === '6.0') { + cy.get( + '.typography-block-support-panel .components-button[aria-label="View and add options"]', + ).click(); + + cy.get('.components-button[aria-label="Show Font size"]').click(); + cy.get('.components-custom-select-control__button[aria-label="Font size"]').click(); + cy.get('.components-custom-select-control__item').contains('Extra small').click(); + + cy.get( + '.typography-block-support-panel .components-button[aria-label="View options"]', + ).click(); + cy.get('.components-button[aria-label="Show Line height"]').click(); + cy.get('.components-input-control__input[placeholder="1.5"]').clearThenType(2); + } else { + cy.get('.block-editor-block-inspector button[aria-label="Styles"]').click(); + cy.get('.block-editor-block-inspector button[aria-label="Typography options"]').click(); + + cy.get('[aria-label="Typography options"] button, .popover-slot button') + .contains(/Font size|Size/) + .as('fontSizeButton'); + cy.get('@fontSizeButton').click(); + cy.get('@fontSizeButton').click(); + cy.get('@fontSizeButton').type('{esc}'); + + cy.get( + '.block-editor-block-inspector fieldset.components-font-size-picker button[role="combobox"]', + ).click(); + cy.get( + '.block-editor-block-inspector li[role="option"], .block-editor-block-inspector div[role="option"]', + ) + .contains('Extra small') + .click(); + + cy.get('.block-editor-line-height-control input').clearThenType(2); + + cy.get('.block-editor-block-inspector button[aria-label="Settings"]').click(); + } + } + + cy.wrap(subject).should('have.css', 'font-size', '16px'); + cy.wrap(subject).should('have.css', 'line-height', '32px'); +}); + +Cypress.Commands.add('supportsBlockDimensions', { prevSubject: true }, (subject, isEdit) => { + if (isEdit) { + if (wpVersion === '6.0') { + cy.get( + '.dimensions-block-support-panel .components-button[aria-label="View and add options"]', + ).click(); + + cy.get('.components-button[aria-label="Show Padding"]').click(); + cy.get('.components-button[aria-label="Unlink Sides"]').click(); + cy.get('.components-input-control__input[aria-label="Top"]').clearThenType(10); + cy.get('.components-input-control__input[aria-label="Right"]').clearThenType(15); + cy.get('.components-input-control__input[aria-label="Bottom"]').clearThenType(10); + cy.get('.components-input-control__input[aria-label="Left"]').clearThenType(15); + + cy.get('.dimensions-block-support-panel').click(); + } else { + cy.get('.block-editor-block-inspector button[aria-label="Styles"]').click(); + cy.get('.block-editor-block-inspector button[aria-label="Dimensions options"]').click(); + + cy.get('.dimensions-block-support-panel').as('dimensionsPanel'); + + cy.get('[aria-label="Dimensions options"] button, .popover-slot button') + .contains('Padding') + .as('paddingButton'); + cy.get('@paddingButton').click(); + cy.get('@paddingButton').type('{esc}'); + + cy.get('@dimensionsPanel') + .find('.component-spacing-sizes-control, .spacing-sizes-control__wrapper') + .first() + .as('verticalInputsWrapper'); + + cy.get('@verticalInputsWrapper') + .find('button[aria-label="Set custom size"]') + .first() + .click(); + cy.get('@verticalInputsWrapper').find('input[type="number"]').clearThenType('10'); + + cy.get('@dimensionsPanel') + .find('.component-spacing-sizes-control, .spacing-sizes-control__wrapper') + .eq(1) + .as('horizontalInputsWrapper'); + + cy.get('@horizontalInputsWrapper') + .find('button[aria-label="Set custom size"]') + .first() + .click(); + cy.get('@horizontalInputsWrapper').find('input[type="number"]').clearThenType('15'); + + cy.get('.block-editor-block-inspector button[aria-label="Settings"]').click(); + } + } + + cy.wrap(subject).should('have.css', 'padding', '10px 15px'); });