TomLonghurst.Selenium.PlaywrightWebDriver
1.0.42
Prefix Reserved
dotnet add package TomLonghurst.Selenium.PlaywrightWebDriver --version 1.0.42
NuGet\Install-Package TomLonghurst.Selenium.PlaywrightWebDriver -Version 1.0.42
<PackageReference Include="TomLonghurst.Selenium.PlaywrightWebDriver" Version="1.0.42" />
paket add TomLonghurst.Selenium.PlaywrightWebDriver --version 1.0.42
#r "nuget: TomLonghurst.Selenium.PlaywrightWebDriver, 1.0.42"
// Install TomLonghurst.Selenium.PlaywrightWebDriver as a Cake Addin #addin nuget:?package=TomLonghurst.Selenium.PlaywrightWebDriver&version=1.0.42 // Install TomLonghurst.Selenium.PlaywrightWebDriver as a Cake Tool #tool nuget:?package=TomLonghurst.Selenium.PlaywrightWebDriver&version=1.0.42
Selenium.PlaywrightDriver
Use Playwright as if it was a Selenium WebDriver
Installation/Usage
If you're using Selenium v4
- Install package
TomLonghurst.Selenium.PlaywrightWebDriver
If you're using Selenium v3
- Install package
TomLonghurst.Selenium.V3.PlaywrightWebDriver
As per the playwright instructions, you need to run the playwright install script to install browser binaries and such:
pwsh bin/Debug/netX/playwright.ps1 install
This can be achieved in C# by something similar to:
Process.Start(new ProcessStartInfo("pwsh")
{
Arguments = "playwright.ps1 install",
WorkingDirectory = Environment.CurrentDirectory
})!.WaitForExit();
Anywhere you are using concrete types (e.g. ChromeWebDriver
), change them to the IWebDriver
interface
Then just create a PlaywrightWebDriver
and use that!
await using var driver = await PlaywrightWebDriver.CreateAsync();
driver.Url = "https://www.tomlonghurst.com";
driver.FindElement(By.ClassName("logo__img")).Click();
The static Create/CreateAsync
methods can take the Playwright BrowserTypeLaunchOptions
and BrowserNewContextOptions
if you want to customise things like browser type, proxys, mobile emulation, etc.
Your PlaywrightWebDriver
will also expose the underlying instances of IPlaywright
, IBrowser
and IBrowserContext
if you need to use/manipulate those objects for any specific reason.
Motivation
I've come across lots of code bases where there are old, large test suites written in Selenium. These are important for confirming the health of a system, so we can't just delete them. But it'd also be a lot of work to migrate/convert them all.
Selenium supports different WebDrivers through the IWebDriver
contract interface. So I thought, is it possible for me to map the Playwright APIs to the Selenium APIs? If we adhere to that interface, we could just drop in a Playwright driver, and we're not actually using the Selenium tech stack anymore.
What's wrong with Selenium anyway? If you're not experiencing any problems, then great. But here's some things I've experienced:
- It's not asynchronous - So extra pressure on the thread pool. (I know this package isn't either, due to the
IWebDriver
interface, but if this does help you migrate, then you'll be in an async Playwright world!) - It's slow
- Spawning new browser instances just randomly crashes sometimes with unknown errors, or port problems, or connectivity problems
- Waiting for elements/pages is flaky
- Just random "UNKNOWN_ERROR" exceptions!?
Caveats
- The parameters, arguments and objects returned by
ExecuteScript
may be different, and so your code surrounding scripts may need to be tweaked - Alerts auto dismiss. So
Alert.Accept()
orAlert.Dismiss()
don't actually do anything.Alert.SendKeys(text)
is not supported. - The
WebDriver.Manage().Network
functionality has not been implemented - The
WebDriver.Manage().Logs
functionality has not been implemented - Sync-over-async. Playwright is async, but Selenium is not. So this was necessary to adhere to the
IWebDriver
interface. This means that this may be slower than a pure playwright solution due to more pressure on the thread pool. There's also always the risk of sync-over-async deadlocks. As such, this package is recommended as a migration strategy, and you should migrate to the pure playwright API if/when possible.
Bonus
You can start using Playwright selectors even with the Selenium APIs. Instead of using the By.Id(...) class, you can use the PlaywrightBy.Selector(...) class. You can pass this in anywhere you'd pass in a normal By object!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Playwright (>= 1.48.0)
- Selenium.WebDriver (>= 4.25.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.42 | 84 | 10/29/2024 |
1.0.4 | 183 | 4/13/2024 |
1.0.0 | 102 | 4/12/2024 |
0.1.24 | 100 | 4/11/2024 |
0.1.23 | 101 | 4/11/2024 |
0.1.21 | 97 | 4/11/2024 |
0.1.19 | 93 | 4/11/2024 |
0.1.17 | 101 | 4/11/2024 |
0.1.16 | 97 | 4/11/2024 |
0.1.15 | 99 | 4/11/2024 |
0.1.13 | 100 | 4/11/2024 |
0.1.12 | 96 | 4/11/2024 |
0.1.11 | 104 | 4/11/2024 |
0.1.9 | 100 | 4/11/2024 |