diff --git a/src/Playwright/Program.cs b/src/Playwright/Program.cs index f57723a8d..f33b62222 100644 --- a/src/Playwright/Program.cs +++ b/src/Playwright/Program.cs @@ -24,6 +24,7 @@ using System; using System.Diagnostics; +using System.IO; using Microsoft.Playwright.Helpers; namespace Microsoft.Playwright; @@ -37,6 +38,11 @@ public static int Main(string[] args) } public int Run(string[] args) + { + return Run(args, throwOnError: false); + } + + public int Run(string[] args, bool throwOnError) { Func getArgs; string executablePath; @@ -44,7 +50,7 @@ public int Run(string[] args) { (executablePath, getArgs) = Driver.GetExecutablePath(); } - catch + catch when (!throwOnError) { return PrintError("Microsoft.Playwright assembly was found, but is missing required assets. Please ensure to build your project before running Playwright tool."); } @@ -55,6 +61,8 @@ public int Run(string[] args) // This works after net8.0-preview-4 // https://github.com/dotnet/runtime/pull/82662 WindowStyle = ProcessWindowStyle.Hidden, + RedirectStandardOutput = throwOnError, + RedirectStandardError = throwOnError, }; foreach (var pair in Driver.EnvironmentVariables) { @@ -66,9 +74,26 @@ public int Run(string[] args) StartInfo = playwrightStartInfo, }; + using var pwOutput = new StringWriter(); + if (throwOnError) + { + pwProcess.OutputDataReceived += (_, eventArgs) => pwOutput.WriteLine(eventArgs.Data); + pwProcess.ErrorDataReceived += (_, eventArgs) => pwOutput.WriteLine(eventArgs.Data); + } pwProcess.Start(); + if (throwOnError) + { + pwProcess.BeginOutputReadLine(); + pwProcess.BeginErrorReadLine(); + } pwProcess.WaitForExit(); + + if (pwProcess.ExitCode != 0 && throwOnError) + { + throw new PlaywrightException($"Failed to run {playwrightStartInfo.FileName} {playwrightStartInfo.Arguments}{Environment.NewLine}{pwOutput.ToString().Trim()}"); + } + return pwProcess.ExitCode; }