Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion sdk/loadtesting/playwright/review/playwright-node.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export type OsType = (typeof ServiceOS)[keyof typeof ServiceOS];
export type PlaywrightServiceAdditionalOptions = {
serviceAuthType?: AuthenticationType;
os?: OsType;
runId?: string;
timeout?: number;
slowMo?: number;
exposeNetwork?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const os: OsType = ServiceOS.LINUX;
const playwrightServiceAdditionalOptions: PlaywrightServiceAdditionalOptions = {
serviceAuthType: serviceAuthType, // Authentication types supported by Azure Playwright
os: os, // Operating system types supported by Azure Playwright
runId: new Date().toISOString(), // Run id for the test run
timeout: 30000, // Maximum time in milliseconds to wait for the connection to be established
slowMo: 0, // Slows down Playwright operations by the specified amount of milliseconds
exposeNetwork: "<loopback>", // Exposes network available on the connecting client to the browser being connected to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EnvironmentVariables {
region: string | undefined;
runName: string;
constructor() {
this.runName = process.env["_MPT_SERVICE_RUN_NAME"]!;
this.runName = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME]!;
this.runId = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]!;
this.correlationId = randomUUID();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { state } from "./state.js";

class PlaywrightServiceConfig {
public serviceOs: OsType;
public runId: string;
public timeout: number;
public slowMo: number;
public exposeNetwork: string;
Expand All @@ -26,14 +25,22 @@ class PlaywrightServiceConfig {
this.serviceOs = (process.env[InternalEnvironmentVariables.MPT_SERVICE_OS] ||
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS) as OsType;
this.runName = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] || "";
this.runId = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID] || "";
this.timeout = DefaultConnectOptionsConstants.DEFAULT_TIMEOUT;
this.slowMo = DefaultConnectOptionsConstants.DEFAULT_SLOW_MO;
this.exposeNetwork = DefaultConnectOptionsConstants.DEFAULT_EXPOSE_NETWORK;
this.apiVersion =
process.env[InternalEnvironmentVariables.MPT_API_VERSION] || Constants.LatestAPIVersion;
}

private _runId?: string;

public get runId(): string {
if (!this._runId) {
this._runId = getAndSetRunId();
}
return this._runId;
}

public static get instance(): PlaywrightServiceConfig {
if (!state.playwrightServiceConfig) {
state.playwrightServiceConfig = new PlaywrightServiceConfig();
Expand Down Expand Up @@ -66,14 +73,6 @@ class PlaywrightServiceConfig {
}
this.apiVersion = options.apiVersion;
}
if (!process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]) {
if (options?.runId) {
this.runId = options.runId;
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID] = this.runId;
} else {
this.runId = getAndSetRunId();
}
}
if (!process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] && options?.runName) {
this.runName = options.runName;
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] = this.runName;
Expand Down
9 changes: 0 additions & 9 deletions sdk/loadtesting/playwright/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ export type PlaywrightServiceAdditionalOptions = {
*/
os?: OsType;

/**
* @public
*
* Run id for the test run.
*
* @defaultValue `current datetime as ISO string`
*/
runId?: string;

/**
* @public
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
InternalEnvironmentVariables,
} from "../../src/common/constants.js";
import { PlaywrightServiceConfig } from "../../src/common/playwrightServiceConfig.js";
import { getAndSetRunId } from "../../src/utils/utils.js";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";

describe("PlaywrightServiceConfig", () => {
Expand All @@ -21,13 +20,20 @@ describe("PlaywrightServiceConfig", () => {
});

it("should set service config object with default values", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
const playwrightServiceConfig = new PlaywrightServiceConfig();

expect(playwrightServiceConfig.serviceOs).to.equal(
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
);
expect(playwrightServiceConfig.runName).to.equal("");
expect(playwrightServiceConfig.runId).to.equal("");

const firstRunId = playwrightServiceConfig.runId;
expect(firstRunId).to.be.a("string");
expect(firstRunId.length).to.be.greaterThan(0);

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(firstRunId);

expect(playwrightServiceConfig.timeout).to.equal(
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
);
Expand All @@ -36,7 +42,6 @@ describe("PlaywrightServiceConfig", () => {
expect(playwrightServiceConfig.exposeNetwork).to.equal(
DefaultConnectOptionsConstants.DEFAULT_EXPOSE_NETWORK,
);
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).toBeUndefined();

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_OS];
Expand All @@ -51,7 +56,14 @@ describe("PlaywrightServiceConfig", () => {
const playwrightServiceConfig = new PlaywrightServiceConfig();

expect(playwrightServiceConfig.serviceOs).to.equal("windows");
expect(playwrightServiceConfig.runId).to.equal("runId");

const currentRunId = playwrightServiceConfig.runId;
expect(currentRunId).to.be.a("string");
expect(currentRunId.length).to.be.greaterThan(0);
expect(currentRunId).to.not.equal("runId");

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(currentRunId);

expect(playwrightServiceConfig.runName).to.equal("runName");
expect(playwrightServiceConfig.apiVersion).to.equal("api-version");

Expand All @@ -62,10 +74,11 @@ describe("PlaywrightServiceConfig", () => {
});

it("should set service config object with values from options", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];

const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
os: "windows",
runId: "runId",
runName: "runName",
slowMo: 100,
timeout: 200,
Expand All @@ -74,13 +87,19 @@ describe("PlaywrightServiceConfig", () => {
});

expect(playwrightServiceConfig.serviceOs).to.equal("windows");
expect(playwrightServiceConfig.runId).to.equal("runId");

const currentRunId = playwrightServiceConfig.runId;
expect(currentRunId).to.be.a("string");
expect(currentRunId.length).to.be.greaterThan(0);

expect(playwrightServiceConfig.runName).to.equal("runName");
expect(playwrightServiceConfig.slowMo).to.equal(100);
expect(playwrightServiceConfig.timeout).to.equal(200);
expect(playwrightServiceConfig.exposeNetwork).to.equal("localhost");
expect(playwrightServiceConfig.apiVersion).to.equal("sample");
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal("runId");

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(currentRunId);

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_OS]).to.equal("windows");
expect(process.env[InternalEnvironmentVariables.MPT_API_VERSION]).to.equal("sample");

Expand All @@ -91,13 +110,19 @@ describe("PlaywrightServiceConfig", () => {
});

it("should not set service config object with options if not provided", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];

const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions();

expect(playwrightServiceConfig.serviceOs).to.equal(
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
);
expect(playwrightServiceConfig.runId).toBeDefined();

const currentRunId = playwrightServiceConfig.runId;
expect(currentRunId).to.be.a("string");
expect(currentRunId.length).to.be.greaterThan(0);

expect(playwrightServiceConfig.runName).to.equal("");
expect(playwrightServiceConfig.timeout).to.equal(
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
Expand All @@ -107,9 +132,8 @@ describe("PlaywrightServiceConfig", () => {
DefaultConnectOptionsConstants.DEFAULT_EXPOSE_NETWORK,
);
expect(playwrightServiceConfig.apiVersion).to.equal(Constants.LatestAPIVersion);
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(
playwrightServiceConfig.runId,
);

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(currentRunId);

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_OS];
Expand Down Expand Up @@ -138,36 +162,36 @@ describe("PlaywrightServiceConfig", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_OS];
});
it("should set runId from options if provided and environment variable is not set", () => {
const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
runId: "custom-run-id",
});
expect(playwrightServiceConfig.runId).to.equal("custom-run-id");
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal("custom-run-id");
it("should generate runId automatically when environment variable is not set", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
});
it("should generate runId if not provided in options and environment variable is not set", () => {
const runId = getAndSetRunId();

const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
runId: runId,
});
expect(playwrightServiceConfig.runId).to.be.a("string");
expect(playwrightServiceConfig.runId).to.equal(runId);
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(runId);

const currentRunId = playwrightServiceConfig.runId;
expect(currentRunId).to.be.a("string");
expect(currentRunId.length).to.be.greaterThan(0);

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(currentRunId);

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
});
it("should use runId from environment variable if already set", () => {

it("should use the cached runId after first access", () => {
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID] = "existing-run-id";

const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
runId: "option-run-id",
});
expect(playwrightServiceConfig.runId).to.equal("existing-run-id");
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(
"existing-run-id",
);

const firstRunId = playwrightServiceConfig.runId;
expect(firstRunId).to.be.a("string");
expect(firstRunId).to.not.equal("existing-run-id");

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(firstRunId);

const secondRunId = playwrightServiceConfig.runId;
expect(secondRunId).to.equal(firstRunId);

expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal(firstRunId);

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
});
});
20 changes: 16 additions & 4 deletions sdk/loadtesting/playwright/test/core/playwrightService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ describe("getServiceConfig", () => {
it("should return service config with service connect options and global setup and teardown as list when playwright version is 1.49.0", async () => {
vi.stubEnv(ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_ACCESS_TOKEN, "token");
vi.stubEnv(InternalEnvironmentVariables.MPT_PLAYWRIGHT_VERSION, "1.49.0");
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getServiceConfig: localGetServiceConfig } = await import(
"../../src/core/playwrightService.js"
);
Expand Down Expand Up @@ -120,6 +122,8 @@ describe("getServiceConfig", () => {
it("should return service config with service connect options and global setup and teardown as list when playwright version is 1.49.0 and input global files are string", async () => {
vi.stubEnv(ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_ACCESS_TOKEN, "token");
vi.stubEnv(InternalEnvironmentVariables.MPT_PLAYWRIGHT_VERSION, "1.49.0");
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getServiceConfig: localGetServiceConfig } = await import(
"../../src/core/playwrightService.js"
);
Expand Down Expand Up @@ -153,6 +157,8 @@ describe("getServiceConfig", () => {
it("should return service config with service connect options and global setup and teardown as list when playwright version is 1.49.0 and input global files are list", async () => {
vi.stubEnv(ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_ACCESS_TOKEN, "token");
vi.stubEnv(InternalEnvironmentVariables.MPT_PLAYWRIGHT_VERSION, "1.49.0");
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getServiceConfig: localGetServiceConfig } = await import(
"../../src/core/playwrightService.js"
);
Expand Down Expand Up @@ -238,16 +244,17 @@ describe("getServiceConfig", () => {

it("should set service config options as passed", async () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getServiceConfig: localGetServiceConfig } = await import(
"../../src/core/playwrightService.js"
);
localGetServiceConfig(samplePlaywrightConfigInput, {
os: ServiceOS.WINDOWS,
runId: "1234",
});
const playwrightServiceConfig = new PlaywrightServiceConfig();
expect(playwrightServiceConfig.serviceOs).to.equal(ServiceOS.WINDOWS);
expect(playwrightServiceConfig.runId).to.equal("1234");
expect(playwrightServiceConfig.runId).to.equal("mocked-run-id-12345");
});

it("should set service global setup and teardown for entra authentication", async () => {
Expand Down Expand Up @@ -338,6 +345,8 @@ describe("getServiceConfig", () => {

it("should return service config with service connect options", async () => {
vi.stubEnv(ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_ACCESS_TOKEN, "token");
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getServiceConfig: localGetServiceConfig } = await import(
"../../src/core/playwrightService.js"
);
Expand Down Expand Up @@ -416,13 +425,14 @@ describe("getConnectOptions", () => {

it("should set service connect options with passed values", async () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const { getConnectOptions } = await import("../../src/core/playwrightService.js");
await getConnectOptions({
runId: "1234",
os: ServiceOS.WINDOWS,
});
const playwrightServiceConfig = new PlaywrightServiceConfig();
expect(playwrightServiceConfig.runId).to.equal("1234");
expect(playwrightServiceConfig.runId).to.equal("mocked-run-id-12345");
expect(playwrightServiceConfig.serviceOs).to.equal(ServiceOS.WINDOWS);
});

Expand All @@ -431,6 +441,8 @@ describe("getConnectOptions", () => {
const mockVersion = "1.0.0";
// eslint-disable-next-line @typescript-eslint/no-require-imports
vi.spyOn(require("../../package.json"), "version", "get").mockReturnValue(mockVersion);
const mockRunId = "mocked-run-id-12345";
vi.spyOn(utils, "getAndSetRunId").mockReturnValue(mockRunId);
const connectOptions = await getConnectOptions({});
const playwrightServiceConfig = new PlaywrightServiceConfig();
expect(connectOptions).to.deep.equal({
Expand Down