1- using System . Diagnostics ;
2- using System . Text ;
3- using Microsoft . Playwright ;
1+ using Microsoft . Playwright ;
42
53namespace Swashbuckle . AspNetCore . IntegrationTests ;
64
75public sealed class PlaywrightFixture : IAsyncLifetime
86{
97 private bool _installed ;
108
11- public async ValueTask InitializeAsync ( )
12- => await EnsureInstalled ( ) ;
9+ public ValueTask InitializeAsync ( )
10+ {
11+ EnsureInstalled ( ) ;
12+ return ValueTask . CompletedTask ;
13+ }
1314
1415 public ValueTask DisposeAsync ( )
1516 {
@@ -19,14 +20,14 @@ public ValueTask DisposeAsync()
1920
2021 public async Task VerifyPage ( string url , Func < IPage , Task > test )
2122 {
22- await EnsureInstalled ( ) ;
23+ EnsureInstalled ( ) ;
2324
2425 using var playwright = await Playwright . CreateAsync ( ) ;
2526 var browserType = playwright [ BrowserType . Chromium ] ;
2627
2728 var options = new BrowserTypeLaunchOptions ( ) ;
2829
29- if ( Debugger . IsAttached )
30+ if ( System . Diagnostics . Debugger . IsAttached )
3031 {
3132 options . Args = [ "--auto-open-devtools-for-tabs" ] ;
3233 options . Headless = false ;
@@ -43,109 +44,18 @@ public async Task VerifyPage(string url, Func<IPage, Task> test)
4344 await test ( page ) ;
4445 }
4546
46- private async Task EnsureInstalled ( )
47+ private void EnsureInstalled ( )
4748 {
4849 if ( ! _installed )
4950 {
50- var startInfo = new ProcessStartInfo ( "pwsh" , [ "playwright.ps1" , "install" , "chromium" , "--only-shell" , "--with-deps" ] )
51- {
52- RedirectStandardError = true ,
53- RedirectStandardOutput = true ,
54- } ;
55-
56- using var process = Process . Start ( startInfo ) ;
51+ int result = Microsoft . Playwright . Program . Main ( [ "install" , "chromium" , "--only-shell" , "--with-deps" ] ) ;
5752
58- using var outputTokenSource = new CancellationTokenSource ( ) ;
59-
60- var readOutput = ReadOutputAsync ( process , outputTokenSource . Token ) ;
61-
62- try
53+ if ( result != 0 )
6354 {
64- await process . WaitForExitAsync ( ) ;
65- }
66- catch ( OperationCanceledException )
67- {
68- try
69- {
70- process . Kill ( entireProcessTree : true ) ;
71- }
72- catch ( Exception )
73- {
74- // Ignore
75- }
76- }
77- finally
78- {
79- await outputTokenSource . CancelAsync ( ) ;
80- }
81-
82- ( string error , string output ) = await readOutput ;
83-
84- if ( process . ExitCode != 0 )
85- {
86- throw new InvalidOperationException (
87- $ """
88- Failed to install Playwright dependencies: { process . ExitCode } .
89-
90- Output: { output }
91-
92- Error: { error }
93- """ ) ;
94-
55+ throw new InvalidOperationException ( $ "Failed to install Playwright dependencies: { result } .") ;
9556 }
9657
9758 _installed = true ;
9859 }
99-
100- static async Task < ( string Error , string Output ) > ReadOutputAsync (
101- Process process ,
102- CancellationToken cancellationToken )
103- {
104- var processErrors = ConsumeStreamAsync ( process . StandardError , process . StartInfo . RedirectStandardError , cancellationToken ) ;
105- var processOutput = ConsumeStreamAsync ( process . StandardOutput , process . StartInfo . RedirectStandardOutput , cancellationToken ) ;
106-
107- await Task . WhenAll ( processErrors , processOutput ) ;
108-
109- string error = string . Empty ;
110- string output = string . Empty ;
111-
112- if ( processErrors . Status == TaskStatus . RanToCompletion )
113- {
114- error = ( await processErrors ) . ToString ( ) ;
115- }
116-
117- if ( processOutput . Status == TaskStatus . RanToCompletion )
118- {
119- output = ( await processOutput ) . ToString ( ) ;
120- }
121-
122- return ( error , output ) ;
123- }
124-
125- static Task < StringBuilder > ConsumeStreamAsync (
126- StreamReader reader ,
127- bool isRedirected ,
128- CancellationToken cancellationToken )
129- {
130- return Task . Run ( ( ) => ProcessStream ( reader , cancellationToken ) , cancellationToken ) ;
131-
132- static async Task < StringBuilder > ProcessStream (
133- StreamReader reader ,
134- CancellationToken cancellationToken )
135- {
136- var builder = new StringBuilder ( ) ;
137-
138- try
139- {
140- builder . Append ( await reader . ReadToEndAsync ( cancellationToken ) ) ;
141- }
142- catch ( OperationCanceledException )
143- {
144- // Ignore
145- }
146-
147- return builder ;
148- }
149- }
15060 }
15161}
0 commit comments