From d86cfceda23c281a50afb6f3ae9624fb14e97cce Mon Sep 17 00:00:00 2001 From: Veljko Date: Wed, 18 Sep 2024 16:31:50 +0200 Subject: [PATCH] Make themes util compatible with ext env and unskip relevant tests --- .../e2e-external-fix-activate-theme-util | 4 + .../tests/customize-store/intro.spec.js | 81 +++++++++---------- .../customize-store/transitional.spec.js | 21 +++-- .../tests/shopper/launch-your-store.spec.js | 2 +- .../woocommerce/tests/e2e-pw/utils/themes.js | 59 ++++++++++++-- 5 files changed, 102 insertions(+), 65 deletions(-) create mode 100644 plugins/woocommerce/changelog/e2e-external-fix-activate-theme-util diff --git a/plugins/woocommerce/changelog/e2e-external-fix-activate-theme-util b/plugins/woocommerce/changelog/e2e-external-fix-activate-theme-util new file mode 100644 index 00000000000..47fe465eb1e --- /dev/null +++ b/plugins/woocommerce/changelog/e2e-external-fix-activate-theme-util @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +E2E tests - fixing activateTheme util to work on wp env and external envs \ No newline at end of file diff --git a/plugins/woocommerce/tests/e2e-pw/tests/customize-store/intro.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/customize-store/intro.spec.js index 22d7c7d449d..449add25331 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/customize-store/intro.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/customize-store/intro.spec.js @@ -58,24 +58,21 @@ test.describe( ); } ); - test( - 'it shows the "offline banner" when the network is offline', - { tag: '@skip-on-default-pressable' }, - async ( { page, context } ) => { - await page.goto( CUSTOMIZE_STORE_URL ); - await expect( - page.locator( 'text=Design your own' ) - ).toBeVisible(); - await context.setOffline( true ); + test( 'it shows the "offline banner" when the network is offline', async ( { + page, + context, + } ) => { + await page.goto( CUSTOMIZE_STORE_URL ); + await expect( + page.locator( 'text=Design your own' ) + ).toBeVisible(); + await context.setOffline( true ); - await expect( page.locator( '.offline-banner' ) ).toBeVisible(); - await expect( - page.locator( - 'text=Looking to design your store using AI?' - ) - ).toBeVisible(); - } - ); + await expect( page.locator( '.offline-banner' ) ).toBeVisible(); + await expect( + page.locator( 'text=Looking to design your store using AI?' ) + ).toBeVisible(); + } ); test( 'it shows the "no AI" banner on Core when the task is not completed', async ( { page, @@ -118,38 +115,34 @@ test.describe( ).toBeVisible(); } ); - test( - 'it shows the "non default block theme" banner when the theme is a block theme different than TT4', - { tag: '@skip-on-default-pressable' }, - async ( { page } ) => { - await activateTheme( 'twentytwentythree' ); + test( 'it shows the "non default block theme" banner when the theme is a block theme different than TT4', async ( { + page, + } ) => { + await activateTheme( 'twentytwentythree' ); - await page.goto( CUSTOMIZE_STORE_URL ); + await page.goto( CUSTOMIZE_STORE_URL ); - await expect( page.locator( 'h1' ) ).toHaveText( - 'Customize your theme' - ); - await expect( - page.getByRole( 'button', { name: 'Go to the Editor' } ) - ).toBeVisible(); - } - ); + await expect( page.locator( 'h1' ) ).toHaveText( + 'Customize your theme' + ); + await expect( + page.getByRole( 'button', { name: 'Go to the Editor' } ) + ).toBeVisible(); + } ); - test( - 'clicking on "Go to the Customizer" with a classic theme should go to the customizer', - { tag: '@skip-on-default-pressable' }, - async ( { page } ) => { - await activateTheme( 'twentytwenty' ); + test( 'clicking on "Go to the Customizer" with a classic theme should go to the customizer', async ( { + page, + } ) => { + await activateTheme( 'twentytwenty' ); - await page.goto( CUSTOMIZE_STORE_URL ); + await page.goto( CUSTOMIZE_STORE_URL ); - await page - .getByRole( 'button', { name: 'Go to the Customizer' } ) - .click(); + await page + .getByRole( 'button', { name: 'Go to the Customizer' } ) + .click(); - await page.waitForNavigation(); - await expect( page.url() ).toContain( 'customize.php' ); - } - ); + await page.waitForNavigation(); + await expect( page.url() ).toContain( 'customize.php' ); + } ); } ); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/customize-store/transitional.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/customize-store/transitional.spec.js index 58f4165f233..b156bec00aa 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/customize-store/transitional.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/customize-store/transitional.spec.js @@ -60,20 +60,17 @@ test.describe( ); } ); - test( - 'Accessing the transitional page when the CYS flow is not completed should redirect to the Intro page', - { tag: '@skip-on-default-pressable' }, - async ( { page, baseURL } ) => { - await page.goto( TRANSITIONAL_URL ); + test( 'Accessing the transitional page when the CYS flow is not completed should redirect to the Intro page', async ( { + page, + baseURL, + } ) => { + await page.goto( TRANSITIONAL_URL ); - const locator = page.locator( 'h1:visible' ); - await expect( locator ).not.toHaveText( - 'Your store looks great!' - ); + const locator = page.locator( 'h1:visible' ); + await expect( locator ).not.toHaveText( 'Your store looks great!' ); - await expect( page.url() ).toBe( `${ baseURL }${ INTRO_URL }` ); - } - ); + await expect( page.url() ).toBe( `${ baseURL }${ INTRO_URL }` ); + } ); test( 'Clicking on "Finish customizing" in the assembler should go to the transitional page', async ( { pageObject, diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/launch-your-store.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/launch-your-store.spec.js index 5db7a5ebd3b..9009f3e6787 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/launch-your-store.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/launch-your-store.spec.js @@ -74,7 +74,7 @@ async function runComingSoonTests( themeContext = '' ) { test.describe( 'Launch Your Store front end - logged out', - { tag: [ '@skip-on-default-wpcom', '@skip-on-default-pressable' ] }, + { tag: '@skip-on-default-wpcom' }, () => { test.afterAll( async ( { baseURL } ) => { try { diff --git a/plugins/woocommerce/tests/e2e-pw/utils/themes.js b/plugins/woocommerce/tests/e2e-pw/utils/themes.js index 0f66505a654..129ca1ae031 100644 --- a/plugins/woocommerce/tests/e2e-pw/utils/themes.js +++ b/plugins/woocommerce/tests/e2e-pw/utils/themes.js @@ -1,18 +1,61 @@ const { exec } = require( 'node:child_process' ); +const { encodeCredentials } = require( '../utils/plugin-utils' ); +const https = require( 'https' ); +const { admin } = require( '../test-data/data' ); +const { BASE_URL } = process.env; export const DEFAULT_THEME = 'twentytwentythree'; export const activateTheme = ( themeName ) => { return new Promise( ( resolve, reject ) => { - const command = `wp-env run tests-cli wp theme install ${ themeName } --activate`; + const isLocalhost = BASE_URL.includes( 'localhost' ); + if ( isLocalhost ) { + // Command for local environment + const command = `wp-env run tests-cli wp theme install ${ themeName } --activate`; - exec( command, ( error, stdout ) => { - if ( error ) { - console.error( `Error executing command: ${ error }` ); - return reject( error ); - } + exec( command, ( error, stdout ) => { + if ( error ) { + console.error( `Error executing command: ${ error }` ); + return reject( error ); + } + resolve( stdout ); + } ); + } else { + // HTTPS request for external environment + const url = new URL( BASE_URL ); + const options = { + hostname: url.hostname, + port: url.port || 443, + path: '/wp-json/custom/v1/activate-theme', + method: 'POST', + headers: { + Authorization: `Basic ${ encodeCredentials( + admin.username, + admin.password + ) }`, + 'Content-Type': 'application/json', + }, + }; - resolve( stdout ); - } ); + const req = https.request( options, ( res ) => { + let data = ''; + + res.on( 'data', ( chunk ) => { + data += chunk; + } ); + + res.on( 'end', () => { + resolve( JSON.parse( data ).message ); + } ); + } ); + + req.on( 'error', ( error ) => { + reject( error ); + } ); + + // Send the request body + req.write( JSON.stringify( { theme_name: themeName } ) ); + req.end(); + } } ); };