E2E Testing with Playwright for Python: Pros and Cons

Playwright is an end-to-end (E2E) testing tool for web based applications. I’ve recently started using Playwright for testing APIs and websites, and I wanted to share my experiences.

Playwright comes in two different flavours. There is ‘standard’ Playwright, which is installed using npm or yarm and involves writing tests in Javascript or Typescript. Then there is Playwright for Python, which is a plugin for the PyTest library, allowing tests to be written in Python 3 using a syntax that is similar to other Python unit tests you might have in your test suite.

I’ll be speaking specifically about Playwright for Python in this blog post, and sharing my personal thoughts and experiences of using the tool, based on some recent projects.

Pros

There are some great benefits that come from using Playwright for Python to write your E2E tests.

For web developers, standard Playwright may be the easier option – particularly if those writing the tests are familiar with Javascript. For backend developers, Playwright for Python provides a way for you to write the same E2E tests without having to learn a new language which reduces the ramp up time to testing and removes a barrier to using Playwright, which in itself has it’s own syntax to learn and understand.

Using Playwright for Python allows you to store and execute your E2E tests in the same suite as your PyTest unit tests. If you already have an automated unit test suite, you won’t have to change the existing infrastructure much to add in your Playwright tests! You will just need to install the pytest-playwright package, which can be easily done using Pip or Conda.

Playwright for Python also has built in Continuous Integration features, allowing you to install Playwright for Python in your CI pipelines using Docker, GitHub actions etc.

Playwright is cross browser and cross platform, meaning it will perform on many different test environments. This is not only great for ensuring your web apps perform under many different circumstances, but can also be really helpful if your company firewalls prevent testing on certain browsers: you can configure certificates for specific browsers to prevent this issue blocking your tests!

Playwright is able to take screenshots and video recordings of your E2E tests, which you can then either walk through in steps or store for later viewing. This is so powerful! It can give other stakeholders visual insight into what your tests look like, be used as test evidence for traceability, and help educate future testers on your test process. I love this about Playwright!

You can also use Playwright for Python to run API tests using its APIRequestContext class to send HTTP(S) requests over a network. When combined with Playwrights built in screenshotting and video recording functionality, this becomes a very powerful way of non-functionally testing your APIs: you can verify performance, look at the usability of the API documentation endpoint, and screenshot the API responses from the web endpoint itself.

Playwright for Python also works well with many other PyTest plugins, meaning you can combine libraries to perform many different kinds of testing on your web applications.

Cons

Like with any other testing tool, there are a couple of minor downsides that come with using Playwright for Python.

Test coverage is a popular metric used with the rest of the PyTest framework – PyTest even has it’s own coverage plugin (pytest-cov) that serves this exact purpose. Using Playwright to improve test coverage in this way is difficult, and in some cases not possible, because executing call backs to your web application does not directly translate into coverage of your source code. There are also some known teething issues with getting coverage to work with Playwright which will require some manual modification to your PyTest configuration.

As mentioned before, Playwright for Python has a Docker integration. There is a Docker image that can be pulled and run to execute Playwright tests. When I attempted to run the Python Playwright tests in this image, I ran into repeated issues with threads and not being able to start a new one. To get around this issue, I had to construct my own Docker image with all of the dependencies built in which was frustrating, but at least possible.

Overall, these issues have not stopped me using Playwright for Python. The tool is relatively easy to use and set up, and integrates well with other frameworks we have in place. Let me know your thoughts – what E2E test tools do you use?

Leave a comment