Skip to content

Commit 57b8380

Browse files
feat(playwrighttesting): Added runName in service config (#31379)
### Packages impacted by this PR ` @azure/microsoft-playwright-testing` ### Issues associated with this PR added RUNNAME field in playwright service config. ### Describe the problem that is addressed by this PR ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --------- Co-authored-by: Siddharth Singha Roy <[email protected]>
1 parent 258b4b1 commit 57b8380

12 files changed

+63
-16
lines changed

sdk/playwrighttesting/microsoft-playwright-testing/review/microsoft-playwright-testing.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type PlaywrightServiceAdditionalOptions = {
6969
exposeNetwork?: string;
7070
useCloudHostedBrowsers?: boolean;
7171
credential?: TokenCredential;
72+
runName?: string;
7273
};
7374

7475
// @public

sdk/playwrighttesting/microsoft-playwright-testing/src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export const TestResultErrorConstants = [
238238
export const InternalEnvironmentVariables = {
239239
MPT_PLAYWRIGHT_VERSION: "_MPT_PLAYWRIGHT_VERSION",
240240
MPT_SETUP_FATAL_ERROR: "_MPT_SETUP_FATAL_ERROR",
241+
MPT_SERVICE_RUN_NAME: "_MPT_SERVICE_RUN_NAME",
241242
MPT_SERVICE_RUN_ID: "_MPT_SERVICE_RUN_ID",
242243
MPT_CLOUD_HOSTED_BROWSER_USED: "_MPT_CLOUD_HOSTED_BROWSER_USED",
243244
};

sdk/playwrighttesting/microsoft-playwright-testing/src/common/environmentVariables.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export class EnvironmentVariables {
1515
correlationId: string | undefined;
1616
shardId: string | undefined;
1717
region: string | undefined;
18+
runName: string;
1819
constructor() {
20+
this.runId = process.env["PLAYWRIGHT_SERVICE_RUN_ID"]!;
21+
this.runName = process.env["_MPT_SERVICE_RUN_NAME"]!;
1922
this.runId = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]!;
2023
this.correlationId = randomUUID();
2124
}

sdk/playwrighttesting/microsoft-playwright-testing/src/common/playwrightServiceConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class PlaywrightServiceConfig {
1515
public timeout: number;
1616
public slowMo: number;
1717
public exposeNetwork: string;
18-
18+
public runName: string;
1919
constructor() {
2020
this.serviceOs = (process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS] ||
2121
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS) as OsType;
22+
this.runName = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] || "";
2223
this.runId = process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID] || "";
2324
this.timeout = DefaultConnectOptionsConstants.DEFAULT_TIMEOUT;
2425
this.slowMo = DefaultConnectOptionsConstants.DEFAULT_SLOW_MO;
@@ -37,6 +38,10 @@ class PlaywrightServiceConfig {
3738
this.runId = getAndSetRunId();
3839
}
3940
}
41+
if (!process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] && options?.runName) {
42+
this.runName = options.runName;
43+
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] = this.runName;
44+
}
4045
if (options?.os) {
4146
this.serviceOs = options.os;
4247
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS] = this.serviceOs;

sdk/playwrighttesting/microsoft-playwright-testing/src/common/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ export type PlaywrightServiceAdditionalOptions = {
228228
* @defaultValue `DefaultAzureCredential`
229229
*/
230230
credential?: TokenCredential;
231+
/**
232+
* @public
233+
*
234+
* Run name for the test run.
235+
*
236+
* @defaultValue `guid`
237+
*/
238+
runName?: string;
231239
};
232240

233241
/**

sdk/playwrighttesting/microsoft-playwright-testing/src/core/playwrightService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const getServiceConfig = (
9696
connectOptions: {
9797
wsEndpoint: getServiceWSEndpoint(
9898
playwrightServiceConfig.runId,
99+
playwrightServiceConfig.runName,
99100
playwrightServiceConfig.serviceOs,
100101
),
101102
headers: {
@@ -148,6 +149,7 @@ const getConnectOptions = async (
148149
return {
149150
wsEndpoint: getServiceWSEndpoint(
150151
playwrightServiceConfig.runId,
152+
playwrightServiceConfig.runName,
151153
playwrightServiceConfig.serviceOs,
152154
),
153155
options: {

sdk/playwrighttesting/microsoft-playwright-testing/src/utils/reporterUtils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,9 @@ class ReporterUtils {
5454

5555
public async getTestRunObject(ciInfo: CIInfo): Promise<TestRun> {
5656
const testRun = new TestRun();
57-
const runName = await this.getRunName(ciInfo);
58-
if (ReporterUtils.isNullOrEmpty(this.envVariables.runId)) {
59-
if (!ReporterUtils.isNullOrEmpty(runName)) {
60-
this.envVariables.runId = runName;
61-
} else {
62-
this.envVariables.runId = randomUUID();
63-
}
64-
}
57+
const runName = this.envVariables.runName || (await this.getRunName(ciInfo));
6558
testRun.testRunId = this.envVariables.runId;
66-
testRun.displayName = ReporterUtils.isNullOrEmpty(runName) ? randomUUID() : runName;
59+
testRun.displayName = ReporterUtils.isNullOrEmpty(runName) ? this.envVariables.runId : runName;
6760
testRun.creatorName = this.envVariables.userName;
6861
testRun.creatorId = this.envVariables.userId!;
6962
testRun.startTime = ReporterUtils.timestampToRFC3339(this.startTime);

sdk/playwrighttesting/microsoft-playwright-testing/src/utils/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export const getAndSetRunId = (): string => {
6868
return runId;
6969
};
7070

71-
export const getServiceWSEndpoint = (runId: string, os: string): string => {
72-
return `${getServiceBaseURL()}?runId=${runId}&os=${os}&api-version=${API_VERSION}`;
71+
export const getServiceWSEndpoint = (runId: string, runName: string, os: string): string => {
72+
return `${getServiceBaseURL()}?runId=${runId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
7373
};
7474

7575
export const validateServiceUrl = (): void => {

sdk/playwrighttesting/microsoft-playwright-testing/test/common/playwrightServiceConfig.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("PlaywrightServiceConfig", () => {
3434
expect(playwrightServiceConfig.serviceOs).to.equal(
3535
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
3636
);
37+
expect(playwrightServiceConfig.runName).to.equal("");
3738
expect(playwrightServiceConfig.runId).to.equal("");
3839
expect(playwrightServiceConfig.timeout).to.equal(
3940
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
@@ -50,35 +51,41 @@ describe("PlaywrightServiceConfig", () => {
5051

5152
it("should set service config object with values from env variables", () => {
5253
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS] = "windows";
54+
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] = "runName";
5355
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID] = "runId";
5456

5557
const playwrightServiceConfig = new PlaywrightServiceConfig();
5658

5759
expect(playwrightServiceConfig.serviceOs).to.equal("windows");
5860
expect(playwrightServiceConfig.runId).to.equal("runId");
61+
expect(playwrightServiceConfig.runName).to.equal("runName");
5962

6063
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
6164
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
65+
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
6266
});
6367

6468
it("should set service config object with values from options", () => {
6569
const playwrightServiceConfig = new PlaywrightServiceConfig();
6670
playwrightServiceConfig.setOptions({
6771
os: "windows",
6872
runId: "runId",
73+
runName: "runName",
6974
slowMo: 100,
7075
timeout: 200,
7176
exposeNetwork: "localhost",
7277
});
7378

7479
expect(playwrightServiceConfig.serviceOs).to.equal("windows");
7580
expect(playwrightServiceConfig.runId).to.equal("runId");
81+
expect(playwrightServiceConfig.runName).to.equal("runName");
7682
expect(playwrightServiceConfig.slowMo).to.equal(100);
7783
expect(playwrightServiceConfig.timeout).to.equal(200);
7884
expect(playwrightServiceConfig.exposeNetwork).to.equal("localhost");
7985
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal("runId");
8086
expect(process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS]).to.equal("windows");
8187

88+
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
8289
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
8390
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
8491
});
@@ -91,6 +98,7 @@ describe("PlaywrightServiceConfig", () => {
9198
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
9299
);
93100
expect(playwrightServiceConfig.runId).to.exist;
101+
expect(playwrightServiceConfig.runName).to.equal("");
94102
expect(playwrightServiceConfig.timeout).to.equal(
95103
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
96104
);
@@ -102,6 +110,30 @@ describe("PlaywrightServiceConfig", () => {
102110
playwrightServiceConfig.runId,
103111
);
104112

113+
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
114+
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
115+
});
116+
it("should set runName from options if provided and environment variable is not set", () => {
117+
const playwrightServiceConfig = new PlaywrightServiceConfig();
118+
playwrightServiceConfig.setOptions({
119+
runName: "custom-run-name",
120+
});
121+
expect(playwrightServiceConfig.runName).to.equal("custom-run-name");
122+
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME]).to.equal(
123+
"custom-run-name",
124+
);
125+
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
126+
});
127+
it("should use runName from environment variable if already set", () => {
128+
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] = "existing-run-name";
129+
const playwrightServiceConfig = new PlaywrightServiceConfig();
130+
playwrightServiceConfig.setOptions();
131+
132+
expect(playwrightServiceConfig.runName).to.equal("existing-run-name");
133+
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME]).to.equal(
134+
"existing-run-name",
135+
);
136+
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
105137
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
106138
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
107139
});

sdk/playwrighttesting/microsoft-playwright-testing/test/core/playwrightService.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe("getServiceConfig", () => {
135135
expect(config).to.deep.equal({
136136
use: {
137137
connectOptions: {
138-
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
138+
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&runName=${playwrightServiceConfig.runName}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
139139
headers: {
140140
Authorization: "Bearer token",
141141
},
@@ -202,7 +202,7 @@ describe("getConnectOptions", () => {
202202
const connectOptions = await getConnectOptions({});
203203
const playwrightServiceConfig = new PlaywrightServiceConfig();
204204
expect(connectOptions).to.deep.equal({
205-
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
205+
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&runName=${playwrightServiceConfig.runName}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
206206
options: {
207207
headers: {
208208
Authorization: "Bearer token",

sdk/playwrighttesting/microsoft-playwright-testing/test/utils/reporterUtils.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("Reporter Utils", () => {
1010
beforeEach(() => {
1111
const envVariablesMock = {
1212
runId: "test-run-id",
13+
runName: "test-run-name",
1314
shardId: "shard-id",
1415
accountId: "account-id",
1516
accessToken: "test-access-token",

sdk/playwrighttesting/microsoft-playwright-testing/test/utils/utils.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ describe("Service Utils", () => {
7474
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL] =
7575
"wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers";
7676
const runId = "2021-10-11T07:00:00.000Z";
77+
const runName = "runName";
7778
const os = "windows";
78-
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${runId}&os=${os}&api-version=${API_VERSION}`;
79-
expect(getServiceWSEndpoint(runId, os)).to.equal(expected);
79+
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${runId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
80+
expect(getServiceWSEndpoint(runId, runName, os)).to.equal(expected);
8081

8182
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL];
8283
});

0 commit comments

Comments
 (0)