From c10e31d55668265a1aca66679c39c1f573a5fad8 Mon Sep 17 00:00:00 2001 From: Siddharth Singha Roy Date: Mon, 25 Nov 2024 21:12:18 +0000 Subject: [PATCH] fix(playwrighttesting): Add support for multiple global files --- .../microsoft-playwright-testing.api.md | 8 +-- .../src/common/customerConfig.ts | 4 +- .../src/common/messages.ts | 5 ++ .../src/common/types.ts | 8 +-- .../global/playwright-service-global-setup.ts | 8 +-- .../playwright-service-global-teardown.ts | 11 ++-- .../src/core/playwrightService.ts | 66 +++++++++++++++++-- 7 files changed, 88 insertions(+), 22 deletions(-) diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/review/microsoft-playwright-testing.api.md b/sdk/playwrighttesting/microsoft-playwright-testing/review/microsoft-playwright-testing.api.md index f1ea04ce4297..0daa1692d5ec 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/review/microsoft-playwright-testing.api.md +++ b/sdk/playwrighttesting/microsoft-playwright-testing/review/microsoft-playwright-testing.api.md @@ -49,14 +49,14 @@ export type PlaywrightConfig = { use?: { connectOptions: BrowserConnectOptions; }; - globalSetup?: string; - globalTeardown?: string; + globalSetup?: string | string[]; + globalTeardown?: string | string[]; }; // @public export type PlaywrightConfigInput = { - globalSetup?: string; - globalTeardown?: string; + globalSetup?: string | string[]; + globalTeardown?: string | string[]; }; // @public diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/customerConfig.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/customerConfig.ts index 57b55b32491a..280e255afe62 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/customerConfig.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/customerConfig.ts @@ -3,8 +3,8 @@ class CustomerConfig { private static instance: CustomerConfig; - public globalSetup?: string; - public globalTeardown?: string; + public globalSetup?: string | string[]; + public globalTeardown?: string | string[]; public static getInstance(): CustomerConfig { if (!CustomerConfig.instance) { diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/messages.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/messages.ts index e360371555a3..8189df416df5 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/messages.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/messages.ts @@ -16,6 +16,11 @@ export const ServiceErrorMessageConstants = { message: "The Playwright version you are using is not supported. See the list of supported versions at https://aka.ms/mpt/supported-versions.", }, + MULTIPLE_SETUP_FILE_PLAYWRIGHT_VERSION_ERROR: { + key: "MultipleSetupFilePlaywrightVersionError", + message: + "The Playwright version you are using does not support multiple setup files. Please update to Playwright version 1.49.0 or higher.", + }, WORKSPACE_MISMATCH_ERROR: { key: "InvalidAccessToken", message: diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/types.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/types.ts index 7f12ff387e27..7877b5a402e8 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/common/types.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/common/types.ts @@ -119,7 +119,7 @@ export type PlaywrightConfigInput = { * * Learn more about {@link https://playwright.dev/docs/test-global-setup-teardown | global setup and teardown}. */ - globalSetup?: string; + globalSetup?: string | string[]; /** * @public @@ -130,7 +130,7 @@ export type PlaywrightConfigInput = { * * Learn more about {@link https://playwright.dev/docs/test-global-setup-teardown | global setup and teardown}. */ - globalTeardown?: string; + globalTeardown?: string | string[]; }; /** @@ -147,8 +147,8 @@ export type PlaywrightConfig = { use?: { connectOptions: BrowserConnectOptions; }; - globalSetup?: string; - globalTeardown?: string; + globalSetup?: string | string[]; + globalTeardown?: string | string[]; }; /** diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-setup.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-setup.ts index 35120702f3a2..fe4a07fc8988 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-setup.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-setup.ts @@ -9,10 +9,10 @@ import customerConfig from "../../common/customerConfig"; const playwrightServiceGlobalSetupWrapper = async (config: FullConfig): Promise => { const rootDir = config.configFile ? dirname(config.configFile!) : process.cwd(); - const customerGlobalSetupFunc = await loadCustomerGlobalFunction( - rootDir, - customerConfig.globalSetup, - ); + let customerGlobalSetupFunc: any = null; + if (customerConfig.globalSetup && typeof customerConfig.globalSetup === "string") { + customerGlobalSetupFunc = await loadCustomerGlobalFunction(rootDir, customerConfig.globalSetup); + } await playwrightServiceEntra.globalSetup(); if (customerGlobalSetupFunc) { diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-teardown.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-teardown.ts index 5f5d3b6ff780..6bcee563f529 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-teardown.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/global/playwright-service-global-teardown.ts @@ -9,10 +9,13 @@ import customerConfig from "../../common/customerConfig"; const playwrightServiceGlobalTeardownWrapper = async (config: FullConfig): Promise => { const rootDir = config.configFile ? dirname(config.configFile!) : process.cwd(); - const customerGlobalTeardownFunc = await loadCustomerGlobalFunction( - rootDir, - customerConfig.globalTeardown, - ); + let customerGlobalTeardownFunc: any = null; + if (customerConfig.globalTeardown && typeof customerConfig.globalTeardown === "string") { + customerGlobalTeardownFunc = await loadCustomerGlobalFunction( + rootDir, + customerConfig.globalTeardown, + ); + } playwrightServiceEntra.globalTeardown(); if (customerGlobalTeardownFunc) { diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/playwrightService.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/playwrightService.ts index 7a030ffc4e30..b0a4c71c04f8 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/core/playwrightService.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/core/playwrightService.ts @@ -21,7 +21,10 @@ import { validateServiceUrl, exitWithFailureMessage, getPackageVersion, + getPlaywrightVersion, + getVersionInfo, } from "../utils/utils"; +import { ServiceErrorMessageConstants } from "../common/messages"; /** * @public @@ -59,14 +62,52 @@ const getServiceConfig = ( ): PlaywrightConfig => { validatePlaywrightVersion(); validateServiceUrl(); + const playwrightVersionInfo = getVersionInfo(getPlaywrightVersion()); + const isMultipleGlobalFileSupported = + playwrightVersionInfo.major >= 1 && playwrightVersionInfo.minor >= 49; if (options?.credential) { playwrightServiceEntra.entraIdAccessToken = options.credential; } + + // if global setup/teardown is string - + // 1. if multiple global file is supported, convert it to array + // 2. wrap playwright-service global setup/teardown with customer provided global setup/teardown + + // if global setup/teardown is array - + // 1. if multiple global file is not supported, throw error + // 2. append playwright-service global setup/teardown with customer provided global setup/teardown if (config.globalSetup) { - customerConfig.globalSetup = config.globalSetup; + if (typeof config.globalSetup === "string") { + if (isMultipleGlobalFileSupported) { + customerConfig.globalSetup = [config.globalSetup]; + } else { + customerConfig.globalSetup = config.globalSetup; + } + } else { + if (!isMultipleGlobalFileSupported) { + throw new Error( + ServiceErrorMessageConstants.MULTIPLE_SETUP_FILE_PLAYWRIGHT_VERSION_ERROR.message, + ); + } + customerConfig.globalSetup = config.globalSetup; + } } + if (config.globalTeardown) { - customerConfig.globalTeardown = config.globalTeardown; + if (typeof config.globalTeardown === "string") { + if (isMultipleGlobalFileSupported) { + customerConfig.globalTeardown = [config.globalTeardown]; + } else { + customerConfig.globalTeardown = config.globalTeardown; + } + } else { + if (!isMultipleGlobalFileSupported) { + throw new Error( + ServiceErrorMessageConstants.MULTIPLE_SETUP_FILE_PLAYWRIGHT_VERSION_ERROR.message, + ); + } + customerConfig.globalTeardown = config.globalTeardown; + } } const playwrightServiceConfig = new PlaywrightServiceConfig(); @@ -78,8 +119,25 @@ const getServiceConfig = ( // mpt PAT requested and set by the customer, no need to setup entra lifecycle handlers validateMptPAT(exitWithFailureMessage); } else { - globalFunctions.globalSetup = require.resolve("./global/playwright-service-global-setup"); - globalFunctions.globalTeardown = require.resolve("./global/playwright-service-global-teardown"); + // If multiple global file is supported, append playwright-service global setup/teardown with customer provided global setup/teardown + if (isMultipleGlobalFileSupported) { + globalFunctions.globalSetup = [require.resolve("./global/playwright-service-global-setup")]; + globalFunctions.globalTeardown = [ + require.resolve("./global/playwright-service-global-teardown"), + ]; + if (customerConfig.globalSetup) { + globalFunctions.globalSetup.push(...(customerConfig.globalSetup as string[])); + } + if (customerConfig.globalTeardown) { + globalFunctions.globalTeardown.push(...(customerConfig.globalTeardown as string[])); + } + } else { + // If multiple global file is not supported, wrap playwright-service global setup/teardown with customer provided global setup/teardown + globalFunctions.globalSetup = require.resolve("./global/playwright-service-global-setup"); + globalFunctions.globalTeardown = require.resolve( + "./global/playwright-service-global-teardown", + ); + } } if (options?.useCloudHostedBrowsers === false) {