public async Task CloseAsync() { if (_runContext.IsBeingDebugged) { Log("CloseAsync(): Debug mode detected, leaving browser open"); return; } else { Log("CloseAsync(): Attempting to close browser..."); } // note you can't use any of the following, since the process to start Chrome // opens the addtional window on the main chrome process and then exits immediately //process.CloseMainWindow(); //process.Close(); //process.Kill(); // this won't work either because Chrome consists of 7 different processes, this will only kill the windowed process //foreach (Process process in Process.GetProcessesByName("chrome")) //{ // if (process.MainWindowHandle == IntPtr.Zero) // continue; // if (process.MainWindowTitle.StartsWith("Courgette Test Runner")) // process.CloseMainWindow(); //} // use CDP to close Chrome (should also work for Chromium, Firefox, Edge 76+ and Opera, but NOT Safari) Log($"Attempting to connect to browser using CDP on port {_chromeDebuggingPort}"); CdpClient cdp = new CdpClient(_chromeDebuggingPort); // wait for browser to close string result = await cdp.CloseBrowserAsync().ConfigureAwait(false); }