Skip to content

Commit b32feff

Browse files
kashish2508guptakashish
andauthored
feat(loadtestservice): Added runName feature (Azure#51425)
* feat(loadtestservice): Added runName feature * UPDATE * Ran script after updating public API --------- Co-authored-by: guptakashish <[email protected]>
1 parent f77027f commit b32feff

File tree

8 files changed

+134
-6
lines changed

8 files changed

+134
-6
lines changed

sdk/loadtestservice/Azure.Developer.Playwright/api/Azure.Developer.Playwright.net8.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public PlaywrightServiceBrowserClientOptions(Azure.Developer.Playwright.Playwrig
2626
public Microsoft.Extensions.Logging.ILogger? Logger { get { throw null; } set { } }
2727
public System.Runtime.InteropServices.OSPlatform OS { get { throw null; } set { } }
2828
public string RunId { get { throw null; } set { } }
29+
public string RunName { get { throw null; } set { } }
2930
public Azure.Developer.Playwright.ServiceAuthType ServiceAuth { get { throw null; } set { } }
3031
public string? ServiceEndpoint { get { throw null; } set { } }
3132
public bool UseCloudHostedBrowsers { get { throw null; } set { } }

sdk/loadtestservice/Azure.Developer.Playwright/api/Azure.Developer.Playwright.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public PlaywrightServiceBrowserClientOptions(Azure.Developer.Playwright.Playwrig
2626
public Microsoft.Extensions.Logging.ILogger? Logger { get { throw null; } set { } }
2727
public System.Runtime.InteropServices.OSPlatform OS { get { throw null; } set { } }
2828
public string RunId { get { throw null; } set { } }
29+
public string RunName { get { throw null; } set { } }
2930
public Azure.Developer.Playwright.ServiceAuthType ServiceAuth { get { throw null; } set { } }
3031
public string? ServiceEndpoint { get { throw null; } set { } }
3132
public bool UseCloudHostedBrowsers { get { throw null; } set { } }

sdk/loadtestservice/Azure.Developer.Playwright/src/Constants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ internal class Constants
151151
internal static readonly string s_invalid_os_error = "Invalid operating system, supported values are 'linux' and 'windows'.";
152152
internal static readonly string s_workspace_mismatch_error = "The provided access token does not match the specified workspace URL. Please verify that both values are correct.";
153153
internal static readonly string s_invalid_service_endpoint_error_message = "The service endpoint provided is invalid. Please verify the endpoint URL and try again.";
154-
internal static readonly string s_playwright_service_runId_length_exceeded_error_message = "Error: The Run Id you provided exceeds 200 characters. Please provide a shorter Run ID.";
154+
internal static readonly string s_playwright_service_runId_not_guid_error_message = "Error: The Run ID must be a valid GUID format. Please provide a valid GUID for the Run ID.";
155155
internal static readonly string s_playwright_service_runName_truncated_warning = "WARNING: Run name exceeds the maximum limit of 200 characters and will be truncated.";
156156
internal static readonly string s_playwright_Version_not_supported_error_message = "The Playwright version you are using does not support playwright workspaces. Please update to Playwright version 1.50.0 or higher.";
157157
internal static readonly string s_playwright_Invalid_version = "The Playwright version you are using is not supported. See the list of supported versions at https://aka.ms/mpt/supported-versions.";
@@ -162,6 +162,7 @@ internal class Constants
162162
internal static readonly string s_playwright_service_expose_network_environment_variable = "PLAYWRIGHT_SERVICE_EXPOSE_NETWORK";
163163
internal static readonly string s_playwright_service_os_environment_variable = "PLAYWRIGHT_SERVICE_OS";
164164
internal static readonly string s_playwright_service_run_id_environment_variable = "PLAYWRIGHT_SERVICE_RUN_ID";
165+
internal static readonly string s_playwright_service_run_name_environment_variable = "PLAYWRIGHT_SERVICE_RUN_NAME";
165166
}
166167

167168
internal class OSConstants

sdk/loadtestservice/Azure.Developer.Playwright/src/PlaywrightServiceBrowserClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ internal PlaywrightServiceBrowserClient(IEnvironment? environment = null, IEntra
9393
// Call getters to set default environment variables if not already set before
9494
_ = _options.OS;
9595
_ = _options.RunId;
96+
_ = _options.RunName;
9697
_ = _options.ExposeNetwork;
9798
_ = _options.ServiceAuth;
9899
_ = _options.UseCloudHostedBrowsers;

sdk/loadtestservice/Azure.Developer.Playwright/src/PlaywrightServiceBrowserClientOptions.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,22 @@ public OSPlatform OS
5757
/// </summary>
5858
public string RunId
5959
{
60-
get => _runId ?? _environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable) ?? _clientUtility.GetDefaultRunId();
60+
get
61+
{
62+
// If runId is not set, try to get it from the environment variable if not present in environment variable then set default value
63+
if (string.IsNullOrEmpty(_runId))
64+
{
65+
var envRunId = _environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable);
66+
_runId = !string.IsNullOrEmpty(envRunId) ? envRunId : _clientUtility.GetDefaultRunId();
67+
}
68+
return _runId!;
69+
}
6170
set
6271
{
72+
if (!string.IsNullOrEmpty(value) && !Guid.TryParse(value, out _))
73+
{
74+
throw new ArgumentException(Constants.s_playwright_service_runId_not_guid_error_message);
75+
}
6376
_runId = value;
6477
// Set run id if not already set in the environment
6578
if (string.IsNullOrEmpty(_environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable)))
@@ -69,6 +82,41 @@ public string RunId
6982
}
7083
}
7184

85+
private string? _runName;
86+
87+
/// <summary>
88+
/// Gets or sets the run name.
89+
/// </summary>
90+
public string RunName
91+
{
92+
get
93+
{
94+
// If runName is not set, try to get it from the environment variable if not present in environment variable then set default value
95+
if (string.IsNullOrEmpty(_runName))
96+
{
97+
var envRunName = _environment.GetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable);
98+
_runName = !string.IsNullOrEmpty(envRunName) ? envRunName : _clientUtility.GetDefaultRunName(RunId);
99+
}
100+
return _runName!;
101+
}
102+
set
103+
{
104+
if (!string.IsNullOrEmpty(value) && value.Length > 200)
105+
{
106+
_runName = value.Substring(0, 200);
107+
}
108+
else
109+
{
110+
_runName = value;
111+
}
112+
// Set run name if not already set in the environment
113+
if (string.IsNullOrEmpty(_environment.GetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable)))
114+
{
115+
_environment.SetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable, _runName);
116+
}
117+
}
118+
}
119+
72120
private string? _exposeNetwork;
73121
/// <summary>
74122
/// Gets or sets the expose network field for remote browsers.
@@ -211,7 +259,7 @@ public PlaywrightServiceBrowserClientOptions(ServiceVersion serviceVersion = Lat
211259
// no-op
212260
}
213261

214-
internal PlaywrightServiceBrowserClientOptions(ServiceVersion serviceVersion, IEnvironment? environment = null, ClientUtilities? clientUtility = null)
262+
internal PlaywrightServiceBrowserClientOptions(ServiceVersion serviceVersion, IEnvironment? environment = null, ILogger? logger = null, ClientUtilities? clientUtility = null)
215263
{
216264
_environment = environment ?? new EnvironmentHandler();
217265
_clientUtility = clientUtility ?? new ClientUtilities(_environment);

sdk/loadtestservice/Azure.Developer.Playwright/src/Utility/ClientUtilities.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ internal string GetDefaultRunId()
5555
return runId;
5656
}
5757

58+
internal string GetDefaultRunName(string runId)
59+
{
60+
var runNameFromEnvironmentVariable = _environment.GetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable);
61+
if (!string.IsNullOrEmpty(runNameFromEnvironmentVariable))
62+
return runNameFromEnvironmentVariable!;
63+
_environment.SetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable, runId);
64+
return runId;
65+
}
66+
5867
internal void ValidateMptPAT(string? authToken, string serviceEndpoint)
5968
{
6069
if (string.IsNullOrEmpty(authToken))

sdk/loadtestservice/Azure.Developer.Playwright/tests/PlaywrightServiceBrowserClientOptionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void RunId_WhenSetAndEnvironmentNotSet_SetsEnvironmentVariable()
6868
environment: environment,
6969
serviceVersion: PlaywrightServiceBrowserClientOptions.ServiceVersion.V2025_07_01_Preview);
7070

71-
var expectedRunId = "new-run-id";
71+
var expectedRunId = "f3a0f9c8-1b4b-4f44-9a77-062d8d4188e4";
7272
options.RunId = expectedRunId;
7373

7474
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable),
@@ -276,14 +276,14 @@ public void OS_WhenEnvironmentVariableNotSet_ReturnsLinux()
276276
public void RunId_WhenSetAndEnvironmentAlreadySet_DoesNotUpdateEnvironment()
277277
{
278278
var environment = new TestEnvironment();
279-
var existingRunId = "existing-run-id";
279+
var existingRunId = "f3a0f9c8-1b4b-4f44-9a77-062d8d4188e4";
280280
environment.SetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable, existingRunId);
281281

282282
_ = new PlaywrightServiceBrowserClientOptions(
283283
environment: environment,
284284
serviceVersion: PlaywrightServiceBrowserClientOptions.ServiceVersion.V2025_07_01_Preview)
285285
{
286-
RunId = "new-run-id"
286+
RunId = "f3a0f9c8-1b4b-4f44-9a77-062d8d418878"
287287
};
288288

289289
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable),

sdk/loadtestservice/Azure.Developer.Playwright/tests/PlaywrightServiceBrowserClientTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,62 @@ public void Constructor_NoServiceParams_SetsDefaultValues()
4141
Assert.AreEqual(client._options.OS, OSPlatform.Linux);
4242
Assert.AreEqual(client._options.ExposeNetwork, Constants.s_default_expose_network);
4343
Assert.That(Guid.TryParse(client._options.RunId, out _), Is.True);
44+
Assert.That(client._options.RunName, Is.EqualTo(client._options.RunId)); // RunName defaults to RunId
4445

4546
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_os_environment_variable.ToString()), Is.EqualTo(Constants.s_default_os));
4647
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_expose_network_environment_variable.ToString()), Is.EqualTo(Constants.s_default_expose_network));
4748
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable.ToString().ToString()), Is.Not.Null);
49+
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable), Is.EqualTo(client._options.RunId));
50+
});
51+
}
52+
53+
[Test]
54+
public void Constructor_CustomRunName_SetsCustomRunNameValue()
55+
{
56+
var environment = new TestEnvironment();
57+
var playwrightVersion = new PlaywrightVersion();
58+
var customRunName = "Custom Run Name";
59+
var clientOptions = new PlaywrightServiceBrowserClientOptions(environment: environment, serviceVersion: PlaywrightServiceBrowserClientOptions.ServiceVersion.V2025_07_01_Preview)
60+
{
61+
RunName = customRunName
62+
};
63+
PlaywrightServiceBrowserClient client = new(environment, options: clientOptions, playwrightVersion: playwrightVersion);
64+
Assert.Multiple(() =>
65+
{
66+
Assert.That(client._options.RunName, Is.EqualTo(customRunName));
67+
Assert.That(environment.GetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable), Is.EqualTo(customRunName));
68+
});
69+
}
70+
71+
[Test]
72+
public void Constructor_RunNameExceedsMaxLength_TruncatesRunName()
73+
{
74+
var environment = new TestEnvironment();
75+
var playwrightVersion = new PlaywrightVersion();
76+
var longRunName = new string('a', 250);
77+
var clientOptions = new PlaywrightServiceBrowserClientOptions(environment: environment, serviceVersion: PlaywrightServiceBrowserClientOptions.ServiceVersion.V2025_07_01_Preview)
78+
{
79+
RunName = longRunName
80+
};
81+
PlaywrightServiceBrowserClient client = new(environment, options: clientOptions, playwrightVersion: playwrightVersion);
82+
Assert.Multiple(() =>
83+
{
84+
Assert.That(client._options.RunName.Length, Is.EqualTo(200));
85+
Assert.That(client._options.RunName, Is.EqualTo(longRunName.Substring(0, 200)));
86+
});
87+
}
88+
89+
[Test]
90+
public void Constructor_RunNameSetFromEnvironment_UsesEnvironmentValue()
91+
{
92+
var environment = new TestEnvironment();
93+
var playwrightVersion = new PlaywrightVersion();
94+
var environmentRunName = "Environment Run Name";
95+
environment.SetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable, environmentRunName);
96+
PlaywrightServiceBrowserClient client = new(environment, playwrightVersion: playwrightVersion);
97+
Assert.Multiple(() =>
98+
{
99+
Assert.That(client._options.RunName, Is.EqualTo(environmentRunName));
48100
});
49101
}
50102

@@ -636,6 +688,18 @@ public void RotationHandler_WhenEntraIdAccessTokenDoesNotRequireRotation_NoOp()
636688
defaultAzureCredentialMock.Verify(x => x.GetTokenAsync(It.IsAny<TokenRequestContext>(), It.IsAny<CancellationToken>()), Times.Never);
637689
}
638690

691+
[Test]
692+
public void SetOptions_WhenRunIdExceedsMaxLength_ThrowsArgumentException()
693+
{
694+
var environment = new TestEnvironment();
695+
var playwrightVersion = new PlaywrightVersion();
696+
var notGuidRunId = new string('a', 201);
697+
var clientOptions = new PlaywrightServiceBrowserClientOptions(environment: environment, serviceVersion: PlaywrightServiceBrowserClientOptions.ServiceVersion.V2025_07_01_Preview);
698+
ArgumentException? exception = Assert.Throws<ArgumentException>(() => clientOptions.RunId = notGuidRunId);
699+
Assert.That(exception, Is.Not.Null);
700+
Assert.That(exception!.Message, Is.EqualTo(Constants.s_playwright_service_runId_not_guid_error_message));
701+
}
702+
639703
[Test]
640704
public void GetConnectOptionsAsync_WhenServiceEndpointIsNotSet_ThrowsException()
641705
{
@@ -849,10 +913,12 @@ public async Task GetConnectOptionsAsync_WhenParametersAreSetViaEnvironmentButAl
849913
public async Task GetConnectOptionsAsync_WhenServiceParametersAreSetViaEnvironment_SetsServiceParameters()
850914
{
851915
var runId = "run-id";
916+
var runName = "run-name";
852917
var environment = new TestEnvironment();
853918
var playwrightVersion = new PlaywrightVersion();
854919
environment.SetEnvironmentVariable(ServiceEnvironmentVariable.PlaywrightServiceUri.ToString(), "https://playwright.microsoft.com");
855920
environment.SetEnvironmentVariable(Constants.s_playwright_service_run_id_environment_variable, runId);
921+
environment.SetEnvironmentVariable(Constants.s_playwright_service_run_name_environment_variable, runName);
856922
var defaultAzureCredentialMock = new Mock<DefaultAzureCredential>();
857923
var jsonWebTokenHandlerMock = new Mock<JsonWebTokenHandler>();
858924
environment.SetEnvironmentVariable(ServiceEnvironmentVariable.PlaywrightServiceAccessToken.ToString(), "valid_token");
@@ -871,6 +937,7 @@ public async Task GetConnectOptionsAsync_WhenServiceParametersAreSetViaEnvironme
871937
{
872938
Assert.That(connectOptions.WsEndpoint, Is.EqualTo($"https://playwright.microsoft.com?os={OSConstants.s_wINDOWS}&runId={runId}&api-version=2025-07-01-preview"));
873939
Assert.That(connectOptions.Options!.ExposeNetwork, Is.EqualTo("localhost"));
940+
Assert.That(client._options.RunName, Is.EqualTo(runName));
874941
});
875942
}
876943

0 commit comments

Comments
 (0)