1212using Azure . Developer . MicrosoftPlaywrightTesting . TestLogger . Interface ;
1313using Azure . Developer . MicrosoftPlaywrightTesting . TestLogger . Implementation ;
1414using Azure . Developer . MicrosoftPlaywrightTesting . TestLogger . Processor ;
15+ using Microsoft . IdentityModel . JsonWebTokens ;
16+ using Microsoft . IdentityModel . Tokens ;
1517
1618namespace Azure . Developer . MicrosoftPlaywrightTesting . TestLogger ;
1719
1820[ FriendlyName ( "microsoft-playwright-testing" ) ]
1921[ ExtensionUri ( "logger://MicrosoftPlaywrightTesting/Logger/v1" ) ]
2022internal class PlaywrightReporter : ITestLoggerWithParameters
2123{
22- private Dictionary < string , string ? > ? _parametersDictionary ;
23- private PlaywrightService ? _playwrightService ;
24- private readonly ILogger _logger ;
25- private TestProcessor ? _testProcessor ;
26-
27- public PlaywrightReporter ( ) : this ( null ) { } // no-op
28- public PlaywrightReporter ( ILogger ? logger )
24+ internal Dictionary < string , string ? > ? _parametersDictionary ;
25+ internal PlaywrightService ? _playwrightService ;
26+ internal TestProcessor ? _testProcessor ;
27+ internal readonly ILogger _logger ;
28+ internal IEnvironment _environment ;
29+ internal IXmlRunSettings _xmlRunSettings ;
30+ internal IConsoleWriter _consoleWriter ;
31+ internal JsonWebTokenHandler _jsonWebTokenHandler ;
32+
33+ public PlaywrightReporter ( ) : this ( null , null , null , null , null ) { } // no-op
34+ public PlaywrightReporter ( ILogger ? logger , IEnvironment ? environment , IXmlRunSettings ? xmlRunSettings , IConsoleWriter ? consoleWriter , JsonWebTokenHandler ? jsonWebTokenHandler )
2935 {
3036 _logger = logger ?? new Logger ( ) ;
37+ _environment = environment ?? new EnvironmentHandler ( ) ;
38+ _xmlRunSettings = xmlRunSettings ?? new XmlRunSettings ( ) ;
39+ _consoleWriter = consoleWriter ?? new ConsoleWriter ( ) ;
40+ _jsonWebTokenHandler = jsonWebTokenHandler ?? new JsonWebTokenHandler ( ) ;
3141 }
3242
3343 public void Initialize ( TestLoggerEvents events , Dictionary < string , string ? > parameters )
@@ -66,18 +76,19 @@ internal void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
6676 }
6777 #endregion
6878
69- private void InitializePlaywrightReporter ( string xmlSettings )
79+ internal void InitializePlaywrightReporter ( string xmlSettings )
7080 {
71- Dictionary < string , object > runParameters = XmlRunSettingsUtilities . GetTestRunParameters ( xmlSettings ) ;
81+ Dictionary < string , object > runParameters = _xmlRunSettings . GetTestRunParameters ( xmlSettings ) ;
82+ Dictionary < string , object > nunitParameters = _xmlRunSettings . GetNUnitParameters ( xmlSettings ) ;
7283 runParameters . TryGetValue ( RunSettingKey . RunId , out var runId ) ;
7384 // If run id is not provided and not set via env, try fetching it from CI info.
74- CIInfo cIInfo = CiInfoProvider . GetCIInfo ( ) ;
75- if ( string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId ) ) )
85+ CIInfo cIInfo = CiInfoProvider . GetCIInfo ( _environment ) ;
86+ if ( string . IsNullOrEmpty ( _environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId ) ) )
7687 {
7788 if ( string . IsNullOrEmpty ( runId ? . ToString ( ) ) )
78- Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId , ReporterUtils . GetRunId ( cIInfo ) ) ;
89+ _environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId , ReporterUtils . GetRunId ( cIInfo ) ) ;
7990 else
80- Environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId , runId ! . ToString ( ) ) ;
91+ _environment . SetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId , runId ! . ToString ( ) ) ;
8192 }
8293 else
8394 {
@@ -89,46 +100,49 @@ private void InitializePlaywrightReporter(string xmlSettings)
89100 runParameters . TryGetValue ( RunSettingKey . ManagedIdentityClientId , out var managedIdentityClientId ) ;
90101 runParameters . TryGetValue ( RunSettingKey . EnableGitHubSummary , out var enableGithubSummary ) ;
91102 runParameters . TryGetValue ( RunSettingKey . EnableResultPublish , out var enableResultPublish ) ;
103+ nunitParameters . TryGetValue ( RunSettingKey . NumberOfTestWorkers , out var numberOfTestWorkers ) ;
92104 string ? enableGithubSummaryString = enableGithubSummary ? . ToString ( ) ;
93105 string ? enableResultPublishString = enableResultPublish ? . ToString ( ) ;
94106
95107 bool _enableGitHubSummary = string . IsNullOrEmpty ( enableGithubSummaryString ) || bool . Parse ( enableGithubSummaryString ! ) ;
96108 bool _enableResultPublish = string . IsNullOrEmpty ( enableResultPublishString ) || bool . Parse ( enableResultPublishString ! ) ;
97109
98- PlaywrightServiceOptions ? playwrightServiceSettings = null ;
110+ PlaywrightServiceOptions ? playwrightServiceSettings ;
99111 try
100112 {
101113 playwrightServiceSettings = new ( runId : runId ? . ToString ( ) , serviceAuth : serviceAuth ? . ToString ( ) , azureTokenCredentialType : azureTokenCredential ? . ToString ( ) , managedIdentityClientId : managedIdentityClientId ? . ToString ( ) ) ;
102114 }
103115 catch ( Exception ex )
104116 {
105- Console . Error . WriteLine ( "Failed to initialize PlaywrightServiceSettings: " + ex ) ;
106- Environment . Exit ( 1 ) ;
117+ _consoleWriter . WriteError ( "Failed to initialize PlaywrightServiceSettings: " + ex ) ;
118+ _environment . Exit ( 1 ) ;
119+ return ;
107120 }
108-
109121 // setup entra rotation handlers
110- _playwrightService = new PlaywrightService ( null , playwrightServiceSettings ! . RunId , null , playwrightServiceSettings . ServiceAuth , null , playwrightServiceSettings . AzureTokenCredential ) ;
122+ _playwrightService = new PlaywrightService ( null , playwrightServiceSettings ! . RunId , null , playwrightServiceSettings . ServiceAuth , null , entraLifecycle : null , jsonWebTokenHandler : _jsonWebTokenHandler , credential : playwrightServiceSettings . AzureTokenCredential ) ;
111123#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
112124 _playwrightService . InitializeAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
113125#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
114126
115- var cloudRunId = Environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId ) ;
116- string baseUrl = Environment . GetEnvironmentVariable ( ReporterConstants . s_pLAYWRIGHT_SERVICE_REPORTING_URL ) ;
117- string accessToken = Environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken ) ;
127+ var cloudRunId = _environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceRunId ) ;
128+ string baseUrl = _environment . GetEnvironmentVariable ( ReporterConstants . s_pLAYWRIGHT_SERVICE_REPORTING_URL ) ;
129+ string accessToken = _environment . GetEnvironmentVariable ( ServiceEnvironmentVariable . PlaywrightServiceAccessToken ) ;
118130 if ( string . IsNullOrEmpty ( baseUrl ) )
119131 {
120- Console . Error . WriteLine ( Constants . s_no_service_endpoint_error_message ) ;
121- Environment . Exit ( 1 ) ;
132+ _consoleWriter . WriteError ( Constants . s_no_service_endpoint_error_message ) ;
133+ _environment . Exit ( 1 ) ;
134+ return ;
122135 }
123136 if ( string . IsNullOrEmpty ( accessToken ) )
124137 {
125- Console . Error . WriteLine ( Constants . s_no_auth_error ) ;
126- Environment . Exit ( 1 ) ;
138+ _consoleWriter . WriteError ( Constants . s_no_auth_error ) ;
139+ _environment . Exit ( 1 ) ;
140+ return ;
127141 }
128142
129143 var baseUri = new Uri ( baseUrl ) ;
130144 var reporterUtils = new ReporterUtils ( ) ;
131- TokenDetails tokenDetails = reporterUtils . ParseWorkspaceIdFromAccessToken ( jsonWebTokenHandler : null , accessToken : accessToken ) ;
145+ TokenDetails tokenDetails = reporterUtils . ParseWorkspaceIdFromAccessToken ( jsonWebTokenHandler : _jsonWebTokenHandler , accessToken : accessToken ) ;
132146 var workspaceId = tokenDetails . aid ;
133147
134148 var cloudRunMetadata = new CloudRunMetadata
@@ -140,6 +154,7 @@ private void InitializePlaywrightReporter(string xmlSettings)
140154 EnableGithubSummary = _enableGitHubSummary ,
141155 TestRunStartTime = DateTime . UtcNow ,
142156 AccessTokenDetails = tokenDetails ,
157+ NumberOfTestWorkers = numberOfTestWorkers != null ? Convert . ToInt32 ( numberOfTestWorkers ) : 1
143158 } ;
144159
145160 _testProcessor = new TestProcessor ( cloudRunMetadata , cIInfo ) ;
0 commit comments