Skip to content

Commit

Permalink
feat(playwrighttesting): Added runName in service config (#31379)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
kashish2508 and Sid200026 authored Oct 17, 2024
1 parent 258b4b1 commit 57b8380
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type PlaywrightServiceAdditionalOptions = {
exposeNetwork?: string;
useCloudHostedBrowsers?: boolean;
credential?: TokenCredential;
runName?: string;
};

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export const TestResultErrorConstants = [
export const InternalEnvironmentVariables = {
MPT_PLAYWRIGHT_VERSION: "_MPT_PLAYWRIGHT_VERSION",
MPT_SETUP_FATAL_ERROR: "_MPT_SETUP_FATAL_ERROR",
MPT_SERVICE_RUN_NAME: "_MPT_SERVICE_RUN_NAME",
MPT_SERVICE_RUN_ID: "_MPT_SERVICE_RUN_ID",
MPT_CLOUD_HOSTED_BROWSER_USED: "_MPT_CLOUD_HOSTED_BROWSER_USED",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export class EnvironmentVariables {
correlationId: string | undefined;
shardId: string | undefined;
region: string | undefined;
runName: string;
constructor() {
this.runId = process.env["PLAYWRIGHT_SERVICE_RUN_ID"]!;
this.runName = process.env["_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 @@ -15,10 +15,11 @@ class PlaywrightServiceConfig {
public timeout: number;
public slowMo: number;
public exposeNetwork: string;

public runName: string;
constructor() {
this.serviceOs = (process.env[ServiceEnvironmentVariable.PLAYWRIGHT_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;
Expand All @@ -37,6 +38,10 @@ class PlaywrightServiceConfig {
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;
}
if (options?.os) {
this.serviceOs = options.os;
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS] = this.serviceOs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ export type PlaywrightServiceAdditionalOptions = {
* @defaultValue `DefaultAzureCredential`
*/
credential?: TokenCredential;
/**
* @public
*
* Run name for the test run.
*
* @defaultValue `guid`
*/
runName?: string;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const getServiceConfig = (
connectOptions: {
wsEndpoint: getServiceWSEndpoint(
playwrightServiceConfig.runId,
playwrightServiceConfig.runName,
playwrightServiceConfig.serviceOs,
),
headers: {
Expand Down Expand Up @@ -148,6 +149,7 @@ const getConnectOptions = async (
return {
wsEndpoint: getServiceWSEndpoint(
playwrightServiceConfig.runId,
playwrightServiceConfig.runName,
playwrightServiceConfig.serviceOs,
),
options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,9 @@ class ReporterUtils {

public async getTestRunObject(ciInfo: CIInfo): Promise<TestRun> {
const testRun = new TestRun();
const runName = await this.getRunName(ciInfo);
if (ReporterUtils.isNullOrEmpty(this.envVariables.runId)) {
if (!ReporterUtils.isNullOrEmpty(runName)) {
this.envVariables.runId = runName;
} else {
this.envVariables.runId = randomUUID();
}
}
const runName = this.envVariables.runName || (await this.getRunName(ciInfo));
testRun.testRunId = this.envVariables.runId;
testRun.displayName = ReporterUtils.isNullOrEmpty(runName) ? randomUUID() : runName;
testRun.displayName = ReporterUtils.isNullOrEmpty(runName) ? this.envVariables.runId : runName;
testRun.creatorName = this.envVariables.userName;
testRun.creatorId = this.envVariables.userId!;
testRun.startTime = ReporterUtils.timestampToRFC3339(this.startTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export const getAndSetRunId = (): string => {
return runId;
};

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

export const validateServiceUrl = (): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("PlaywrightServiceConfig", () => {
expect(playwrightServiceConfig.serviceOs).to.equal(
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
);
expect(playwrightServiceConfig.runName).to.equal("");
expect(playwrightServiceConfig.runId).to.equal("");
expect(playwrightServiceConfig.timeout).to.equal(
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
Expand All @@ -50,35 +51,41 @@ describe("PlaywrightServiceConfig", () => {

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

const playwrightServiceConfig = new PlaywrightServiceConfig();

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

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
});

it("should set service config object with values from options", () => {
const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
os: "windows",
runId: "runId",
runName: "runName",
slowMo: 100,
timeout: 200,
exposeNetwork: "localhost",
});

expect(playwrightServiceConfig.serviceOs).to.equal("windows");
expect(playwrightServiceConfig.runId).to.equal("runId");
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(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID]).to.equal("runId");
expect(process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS]).to.equal("windows");

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
});
Expand All @@ -91,6 +98,7 @@ describe("PlaywrightServiceConfig", () => {
DefaultConnectOptionsConstants.DEFAULT_SERVICE_OS,
);
expect(playwrightServiceConfig.runId).to.exist;
expect(playwrightServiceConfig.runName).to.equal("");
expect(playwrightServiceConfig.timeout).to.equal(
DefaultConnectOptionsConstants.DEFAULT_TIMEOUT,
);
Expand All @@ -102,6 +110,30 @@ describe("PlaywrightServiceConfig", () => {
playwrightServiceConfig.runId,
);

delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
});
it("should set runName from options if provided and environment variable is not set", () => {
const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions({
runName: "custom-run-name",
});
expect(playwrightServiceConfig.runName).to.equal("custom-run-name");
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME]).to.equal(
"custom-run-name",
);
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
});
it("should use runName from environment variable if already set", () => {
process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME] = "existing-run-name";
const playwrightServiceConfig = new PlaywrightServiceConfig();
playwrightServiceConfig.setOptions();

expect(playwrightServiceConfig.runName).to.equal("existing-run-name");
expect(process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME]).to.equal(
"existing-run-name",
);
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_NAME];
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_OS];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe("getServiceConfig", () => {
expect(config).to.deep.equal({
use: {
connectOptions: {
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&runName=${playwrightServiceConfig.runName}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
headers: {
Authorization: "Bearer token",
},
Expand Down Expand Up @@ -202,7 +202,7 @@ describe("getConnectOptions", () => {
const connectOptions = await getConnectOptions({});
const playwrightServiceConfig = new PlaywrightServiceConfig();
expect(connectOptions).to.deep.equal({
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
wsEndpoint: `wss://eastus.playwright.microsoft.com/accounts/1234/browsers?runId=${playwrightServiceConfig.runId}&runName=${playwrightServiceConfig.runName}&os=${playwrightServiceConfig.serviceOs}&api-version=${API_VERSION}`,
options: {
headers: {
Authorization: "Bearer token",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("Reporter Utils", () => {
beforeEach(() => {
const envVariablesMock = {
runId: "test-run-id",
runName: "test-run-name",
shardId: "shard-id",
accountId: "account-id",
accessToken: "test-access-token",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ describe("Service Utils", () => {
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL] =
"wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers";
const runId = "2021-10-11T07:00:00.000Z";
const runName = "runName";
const os = "windows";
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${runId}&os=${os}&api-version=${API_VERSION}`;
expect(getServiceWSEndpoint(runId, os)).to.equal(expected);
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${runId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
expect(getServiceWSEndpoint(runId, runName, os)).to.equal(expected);

delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL];
});
Expand Down

0 comments on commit 57b8380

Please sign in to comment.