Playwright + Chai — that’s all what you need.

mati-qa
2 min readSep 28, 2023

--

Intro

I really like the Playwright framework, and I believe that the fewer dependencies, the better. Playwright is a very powerful and nearly complete tool. Why nearly? Maybe because I prefer the old way of working, or maybe because I’m strange and try to check too much. I think that Playwright could have a better assertion library. While the current library is sufficient for basic tasks, it is not as easy to use when checking table content (or maybe I’m missing something). Anyway, here is the Chai assertion library for your consideration.

What is chai? Directly from the page:

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

It’s so true. It’s easy to use, because of the intuitive chain construction, e.g. expct(something).to.be.empty or
expect(foo).to.have.lengthOf(3). And also easy to integrate.

Integration

How to include it in our project? Easy-peasy, install it using the following command in your project directory:

$ npm install chai

And import it wherever you need it. Of course, we are still able to use both, expect for PW library, and expect (as chaiExpect) from chai — depends on how you import it — have a look at the example below.

Why am I writing about it, because of the one awesome construction, which often save me some time. The members option.

How easy to understand what is happening in that assertion, and plenty of construction/configuration of making different option of the assertion. It’s awesome! Of course the library has much more to offer, and worth to try, but this one is my favourite one.

Conclusion

I remember, each time I was thinking about ‘how to check’ something with PW assertion… Firstly, looking into docs, then trying to solve with the code… Ten checking implementation. And then the chai, just grab the data from UI (e.g. allInnerTexts()), and easily check whatever you wish with the chai. I encourage you to take a closer look at the library, it’s really easy and self-explanatory. So, have fun and experiment. ;)

--

--

No responses yet