Post

Migrating WebDriver tests to Playwright

I keep hearing good things about Playwright, and get a bit frustrated with the hacking around with WebDriver having to wait for things to be visible, scrolling to elements, etc. So I decided to see if I could migrate some existing WebDriver tests to Playwright. I started on this PR.

First of all I tried migrating a couple of tests to Playwright, keeping the same structure of page objects, but re-implementing in Playwright. It went quite well, and the code is a bit simpler (no waiting, scrolling, etc).

But in order to migrate existing tests, we need a plan. It’s unrealistic to migrate all tests at once, so we need a way to migrate incrementally. That’s the way I started - by migrating a couple of tests - but it’s not really a great improvement. And it’s a bit messy because the setup is global and it initializes WebDriver and Playwright. In order to use Playwright, I made a copy of NavigationHelpers called PlaywrightNavigationHelpers, which has a method StartInterviewAsync which is comparable to NavigationHelpers.StartInterview, but it returns a Playwright page object instead of a WebDriver one. This page object (and the nested ones) have the same methods (more or less) as the WebDriver one.

But since this requires a test by test rewrite of the tests, adding more and more methods to the page objects, it seems a bit tedious. Also, it’s not really improving much. We could improve more by reconsidering all the page objects, but then it’s an even bigger job.

So I tried to abstract the page objects in a way that we can globally choose to use either WebDriver or Playwright. I added a setting UsePlaywright (set in user secrets), and NavigationHelpers.StartInterview into StartInterviewAsync, which reads the setting and returns either a WebDriver or a Playwright page object. This means putting an interface around the two implementations of page objects, and having them mirror each other, or more specifically, making the Playwright page objects look like the WebDriver page objects, which I don’t like.

So now I’m thinking I should go back to plan A and rewrite one test at a time, with a bit of thought about making better page objects. But this will be a lot of work, with very little gain, since the WebDriver tests are basically fine.

While writing this, I discovered that I tried a similar thing before and came to the same conclusion as before.

Those who cannot remember the past are condemned to repeat it

This post is licensed under CC BY 4.0 by the author.