Skip to content

Commit

Permalink
fix(playwrighttesting): Add support for multiple global files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid200026 committed Nov 25, 2024
1 parent f1ad6e1 commit c10e31d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[];
};

/**
Expand All @@ -147,8 +147,8 @@ export type PlaywrightConfig = {
use?: {
connectOptions: BrowserConnectOptions;
};
globalSetup?: string;
globalTeardown?: string;
globalSetup?: string | string[];
globalTeardown?: string | string[];
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import customerConfig from "../../common/customerConfig";

const playwrightServiceGlobalSetupWrapper = async (config: FullConfig): Promise<any> => {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import customerConfig from "../../common/customerConfig";

const playwrightServiceGlobalTeardownWrapper = async (config: FullConfig): Promise<void> => {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
validateServiceUrl,
exitWithFailureMessage,
getPackageVersion,
getPlaywrightVersion,
getVersionInfo,
} from "../utils/utils";
import { ServiceErrorMessageConstants } from "../common/messages";

/**
* @public
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit c10e31d

Please sign in to comment.