From 381d75120ac865ccf38ebec7fd5d42ee6586eb76 Mon Sep 17 00:00:00 2001 From: Benjamin Freitag Date: Mon, 8 Aug 2022 14:02:09 +0200 Subject: [PATCH] fix: make sure it works with packages.config (.NET 4.8) Extended Playwright.CreateAsync() with optional driversPath param Fixes #1842, fixes #2240, fixes #2004 --- src/Playwright/Helpers/Driver.cs | 7 ++++++- src/Playwright/Playwright.cs | 5 +++-- src/Playwright/Transport/StdIOTransport.cs | 8 ++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Playwright/Helpers/Driver.cs b/src/Playwright/Helpers/Driver.cs index 055de6b432..3b97491eae 100644 --- a/src/Playwright/Helpers/Driver.cs +++ b/src/Playwright/Helpers/Driver.cs @@ -32,8 +32,13 @@ namespace Microsoft.Playwright.Helpers { internal static class Driver { - internal static string GetExecutablePath() + internal static string GetExecutablePath(string driversPath = null) { + if (driversPath != null) + { + return GetPath(driversPath); + } + DirectoryInfo assemblyDirectory = new(AppContext.BaseDirectory); if (!assemblyDirectory.Exists || !File.Exists(Path.Combine(assemblyDirectory.FullName, "Microsoft.Playwright.dll"))) { diff --git a/src/Playwright/Playwright.cs b/src/Playwright/Playwright.cs index fa57762b9c..0fae412925 100644 --- a/src/Playwright/Playwright.cs +++ b/src/Playwright/Playwright.cs @@ -37,11 +37,12 @@ public static class Playwright /// /// Launches Playwright. /// + /// Playwright driver path. /// A that completes when the playwright driver is ready to be used. - public static async Task CreateAsync() + public static async Task CreateAsync(string driversPath = null) { #pragma warning disable CA2000 // Dispose objects before losing scope - var transport = new StdIOTransport(); + var transport = new StdIOTransport(driversPath); #pragma warning restore CA2000 var connection = new Connection(); transport.MessageReceived += (_, message) => connection.Dispatch(JsonSerializer.Deserialize(message, JsonExtensions.DefaultJsonSerializerOptions)); diff --git a/src/Playwright/Transport/StdIOTransport.cs b/src/Playwright/Transport/StdIOTransport.cs index f37d1949a7..485fbcd581 100644 --- a/src/Playwright/Transport/StdIOTransport.cs +++ b/src/Playwright/Transport/StdIOTransport.cs @@ -39,9 +39,9 @@ internal class StdIOTransport : IConnectionTransport, IDisposable private readonly List _data = new(); private int? _currentMessageSize; - internal StdIOTransport() + internal StdIOTransport(string driversPath = null) { - _process = GetProcess(); + _process = GetProcess(driversPath); _process.StartInfo.Arguments = "run-driver"; _process.Start(); _process.Exited += (_, _) => Close("Process exited"); @@ -114,9 +114,9 @@ public async Task SendAsync(byte[] message) } } - private static Process GetProcess() + private static Process GetProcess(string driversPath = null) { - var startInfo = new ProcessStartInfo(Driver.GetExecutablePath()) + var startInfo = new ProcessStartInfo(Driver.GetExecutablePath(driversPath)) { UseShellExecute = false, RedirectStandardOutput = true,