Payment not tested rest done-ish
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace BadmintonBooker;
|
||||
|
||||
internal static class BrowserManager
|
||||
{
|
||||
private const int DebugPort = 9222;
|
||||
private const string ChromeExe = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
|
||||
private const string UserDataDir = @"C:\Users\Shaamil\PlaywrightData";
|
||||
|
||||
public static async Task<IBrowserContext> GetContextAsync(IPlaywright playwright)
|
||||
{
|
||||
await EnsureChromeRunningAsync();
|
||||
var browser = await playwright.Chromium.ConnectOverCDPAsync($"http://127.0.0.1:{DebugPort}");
|
||||
return browser.Contexts[0];
|
||||
}
|
||||
|
||||
private static async Task EnsureChromeRunningAsync()
|
||||
{
|
||||
if (IsPortListening(DebugPort))
|
||||
return;
|
||||
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = ChromeExe,
|
||||
Arguments = $"--remote-debugging-port={DebugPort} --user-data-dir=\"{UserDataDir}\"",
|
||||
UseShellExecute = true
|
||||
});
|
||||
|
||||
// Wait for Chrome to bind the debug port
|
||||
const int maxWaitMs = 10000;
|
||||
const int intervalMs = 500;
|
||||
var elapsed = 0;
|
||||
|
||||
while (!IsPortListening(DebugPort) && elapsed < maxWaitMs)
|
||||
{
|
||||
await Task.Delay(intervalMs);
|
||||
elapsed += intervalMs;
|
||||
}
|
||||
|
||||
if (!IsPortListening(DebugPort))
|
||||
throw new TimeoutException("Chrome did not open the remote debugging port in time.");
|
||||
}
|
||||
|
||||
private static bool IsPortListening(int port)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var tcp = new TcpClient();
|
||||
tcp.Connect("127.0.0.1", port);
|
||||
return true;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user