1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . IO ;
7
+ using System . Linq ;
8
+ using System . Runtime . Versioning ;
9
+ using System . Text ;
10
+ using System . Threading . Tasks ;
11
+ using Common ;
12
+ using Microsoft . Identity . Lab . Api ;
13
+ using Microsoft . Playwright ;
14
+ using Xunit ;
15
+ using Xunit . Abstractions ;
16
+ using Process = System . Diagnostics . Process ;
17
+ using TC = Common . TestConstants ;
18
+
19
+ namespace MultipleApiUiTest
20
+ {
21
+ public class AnyOrgOrPersonalTest : IClassFixture < InstallPlaywrightBrowserFixture >
22
+ {
23
+ private const string SignOutPageUriPath = @"/MicrosoftIdentity/Account/SignedOut" ;
24
+ private const uint ClientPort = 44321 ;
25
+ private const string TraceFileClassName = "OpenIDConnect" ;
26
+ private const uint NumProcessRetries = 3 ;
27
+ private const string SampleSlnFileName = "1-3-AnyOrgOrPersonal.sln" ;
28
+ private readonly LocatorAssertionsToBeVisibleOptions _assertVisibleOptions = new ( ) { Timeout = 25000 } ;
29
+ private readonly string _sampleAppPath = "1-WebApp-OIDC" + Path . DirectorySeparatorChar + "1-3-AnyOrgOrPersonal" + Path . DirectorySeparatorChar . ToString ( ) ;
30
+ private readonly string _testAppsettingsPath = "UiTests" + Path . DirectorySeparatorChar + "AnyOrgOrPersonalUiTest" + Path . DirectorySeparatorChar . ToString ( ) + TC . AppSetttingsDotJson ;
31
+ private readonly string _testAssemblyLocation = typeof ( AnyOrgOrPersonalTest ) . Assembly . Location ;
32
+ private readonly ITestOutputHelper _output ;
33
+
34
+ public AnyOrgOrPersonalTest ( ITestOutputHelper output )
35
+ {
36
+ _output = output ;
37
+ }
38
+
39
+ [ Fact ]
40
+ [ SupportedOSPlatform ( "windows" ) ]
41
+ public async Task ChallengeUser_MicrosoftIdFlow_LocalApp_ValidEmailPasswordCreds_LoginLogout ( )
42
+ {
43
+ // Setup web app and api environmental variables.
44
+ var clientEnvVars = new Dictionary < string , string >
45
+ {
46
+ { "ASPNETCORE_ENVIRONMENT" , "Development" } ,
47
+ { TC . KestrelEndpointEnvVar , TC . HttpsStarColon + ClientPort }
48
+ } ;
49
+
50
+ Dictionary < string , Process > ? processes = null ;
51
+
52
+ // Arrange Playwright setup, to see the browser UI set Headless = false.
53
+ const string TraceFileName = TraceFileClassName + "_LoginLogout" ;
54
+ using IPlaywright playwright = await Playwright . CreateAsync ( ) ;
55
+ IBrowser browser = await playwright . Chromium . LaunchAsync ( new ( ) { Headless = false } ) ;
56
+ IBrowserContext context = await browser . NewContextAsync ( new BrowserNewContextOptions { IgnoreHTTPSErrors = true } ) ;
57
+ await context . Tracing . StartAsync ( new ( ) { Screenshots = true , Snapshots = true , Sources = true } ) ;
58
+ IPage page = await context . NewPageAsync ( ) ;
59
+ string uriWithPort = TC . LocalhostUrl + ClientPort ;
60
+
61
+ try
62
+ {
63
+ // Build the sample app with correct appsettings file.
64
+ UiTestHelpers . BuildSampleWithTestAppsettings ( _testAssemblyLocation , _sampleAppPath , _testAppsettingsPath , SampleSlnFileName ) ;
65
+
66
+ // Start the web app and api processes.
67
+ // The delay before starting client prevents transient devbox issue where the client fails to load the first time after rebuilding
68
+ var clientProcessOptions = new ProcessStartOptions ( _testAssemblyLocation , _sampleAppPath , TC . s_oidcWebAppExe , clientEnvVars ) ;
69
+
70
+ bool areProcessesRunning = UiTestHelpers . StartAndVerifyProcessesAreRunning ( [ clientProcessOptions ] , out processes , NumProcessRetries ) ;
71
+
72
+ if ( ! areProcessesRunning )
73
+ {
74
+ _output . WriteLine ( $ "Process not started after { NumProcessRetries } attempts.") ;
75
+ StringBuilder runningProcesses = new StringBuilder ( ) ;
76
+ foreach ( var process in processes )
77
+ {
78
+ #pragma warning disable CA1305 // Specify IFormatProvider
79
+ runningProcesses . AppendLine ( $ "Is { process . Key } running: { UiTestHelpers . ProcessIsAlive ( process . Value ) } ") ;
80
+ #pragma warning restore CA1305 // Specify IFormatProvider
81
+ }
82
+ Assert . Fail ( TC . WebAppCrashedString + " " + runningProcesses . ToString ( ) ) ;
83
+ }
84
+
85
+ LabResponse labResponse = await LabUserHelper . GetSpecificUserAsync ( TC . OIDCUser ) ;
86
+
87
+ // Initial sign in
88
+ _output . WriteLine ( "Starting web app sign-in flow." ) ;
89
+ string email = labResponse . User . Upn ;
90
+ await UiTestHelpers . NavigateToWebApp ( uriWithPort , page ) ;
91
+ await UiTestHelpers . EnterEmailAsync ( page , email , _output ) ;
92
+ await UiTestHelpers . EnterPasswordAsync ( page , labResponse . User . GetOrFetchPassword ( ) , _output ) ;
93
+ await Assertions . Expect ( page . GetByText ( "Integrating Azure AD V2" ) ) . ToBeVisibleAsync ( _assertVisibleOptions ) ;
94
+ await Assertions . Expect ( page . GetByText ( email ) ) . ToBeVisibleAsync ( _assertVisibleOptions ) ;
95
+ _output . WriteLine ( "Web app sign-in flow successful." ) ;
96
+
97
+ // Sign out
98
+ _output . WriteLine ( "Starting web app sign-out flow." ) ;
99
+ await page . GetByRole ( AriaRole . Link , new ( ) { Name = "Sign out" } ) . ClickAsync ( ) ;
100
+ await UiTestHelpers . PerformSignOut_MicrosoftIdFlow ( page , email , TC . LocalhostUrl + ClientPort + SignOutPageUriPath , _output ) ;
101
+ _output . WriteLine ( "Web app sign out successful." ) ;
102
+ }
103
+ catch ( Exception ex )
104
+ {
105
+ // Adding guid in case of multiple test runs. This will allow screenshots to be matched to their appropriate test runs.
106
+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
107
+ try
108
+ {
109
+ if ( page != null )
110
+ {
111
+ await page . ScreenshotAsync ( new PageScreenshotOptions ( ) { Path = $ "ChallengeUser_MicrosoftIdFlow_LocalApp_ValidEmailPasswordCreds_TodoAppFunctionsCorrectlyScreenshotFail{ guid } .png", FullPage = true } ) ;
112
+ }
113
+ }
114
+ catch
115
+ {
116
+ _output . WriteLine ( "No Screenshot." ) ;
117
+ }
118
+
119
+ string runningProcesses = UiTestHelpers . GetRunningProcessAsString ( processes ) ;
120
+ Assert . Fail ( $ "the UI automation failed: { ex } output: { ex . Message } .\n { runningProcesses } \n Test run: { guid } ") ;
121
+ }
122
+ finally
123
+ {
124
+ // Make sure all processes and their children are stopped.
125
+ UiTestHelpers . EndProcesses ( processes ) ;
126
+
127
+ // Stop tracing and export it into a zip archive.
128
+ string path = UiTestHelpers . GetTracePath ( _testAssemblyLocation , TraceFileName ) ;
129
+ await context . Tracing . StopAsync ( new ( ) { Path = path } ) ;
130
+ _output . WriteLine ( $ "Trace data for { TraceFileName } recorded to { path } .") ;
131
+
132
+ // Close the browser and stop Playwright.
133
+ await browser . CloseAsync ( ) ;
134
+ playwright . Dispose ( ) ;
135
+ }
136
+ }
137
+
138
+ }
139
+ }
0 commit comments