r/programming • • Sep 07 '22

Rewriting tests from Cypress to Playwright using GPT3

https://contra.com/p/PWBcPYZc-rewriting-tests-from-cypress-to-playwright-using-gpt-3
557 Upvotes

30 comments sorted by

24

u/zephyrtr Sep 07 '22

I'm more interested why playwright is so good?

86

u/gajus0 Sep 07 '22 edited Sep 07 '22

TLDR

  • it is about 10x faster (at least 7x) in our tests

  • it is a lot easier to parallelize tests

  • it is a lot easier to debug tests in Playwright

  • it is a lot easier to write tests

  • it is a lot easier to multiplayer applications (e.g. chat)

Something to keep in mind when comparing the two projects, Cypress is a commercial product with 40M+ in funding, and Playwright is an open-source project with no commercial model behind it. Cypress spends a lot on creating hype around their product, tutorials, conferences, etc. If Cypress spent half as much on their codebase as they are spending on their dev advocates (of which there are many), maybe it would not be such a shit framework.

Each point deserves an article of its own, but to quickly run through each...

it is about 10x faster (at least 7x) in our tests

it is a lot easier to parallelize tests

Cypress has a ton of stupid shit in their codebase that slow down application testing. HTTP1 proxy that everything goes through is the main offender. Just this alone accounts for a massive slowdown of tests. You can disable it (at the cost of breaking some Cypress features), but it is not a documented feature.

It is also a lot slower simply because you can only parallelize test execution at the spec-level (file), whereas Playwright allows to parallelize at test level.

it is a lot easier to debug tests in Playwright

Playwright provides HAR-like explorer that integrates every step executed by Playwright, every assertion, every network request, etc. You couldn't ask for a better experience. See https://playwright.dev/docs/trace-viewer

Cypress has a weird mixmash of UI and console collaborating to print execution context for each step. You are mostly forced to weird workarounds like stringifying errors to get information about what happened.

it is a lot easier to write tests

  • Cypress requires to write weird abstractions using commands and operations. This is because the code needs to be injected into browser environment. If there are errors, their stack traces are meaningless. Playwright is just Node.js.

  • Then you have to extend TypeScript interface that may or may not match your commands/operations.

  • Then you have weird cy. chaining that Cypress team advocates as "easier to understand" code concept for non-tech savvy. In Playwright it is just async/await.

it is a lot easier to multiplayer applications

You can open multiple separate browser contexts in Playwright in parallel as part of the same test, in Cypress you cannot.

31

u/coolcosmos Sep 08 '22

Playwright is an open-source project with no commercial model behind it.

Playwright is a Microsoft product, a multi-billion dollar company.

7

u/Kevin_Jim Sep 08 '22

Playwright is also a continuation of Puppeteer, and it’s available both for JS and Python with good docs.

Of all the testing/automation frameworks, I find it to be the best one overall.

7

u/gajus0 Sep 08 '22

As the other user pointed out, my point was that Cypress (as a company) survival depends on the success of Cypress.

Playwright was initiated by MS team as a fork of Puppeteer to support their other business functions.

There is nothing wrong with either of the origins, but one of them has a lot greater incentive to spread the gospel of their product over the other. This was only meant as a caution to those who go search for articles "Cypress vs Playwright" or hear about Cypress in conferences.

23

u/hbgoddard Sep 08 '22

That doesn't contradict what they said

2

u/AlienVsRedditors Sep 08 '22

Another reason against Cypress is that it seems to break on every release.

I really dread whenever I have to upgrade because I know there will be many, many bugs - sometimes as many as they've fixed!

2

u/gajus0 Sep 08 '22

It is mostly because of a huge tech debt that is hiding in their repository.

0

u/[deleted] Sep 08 '22

[deleted]

1

u/qalc Sep 08 '22

Is the fact it's all method chain-based just a byproduct of its age? To me it's such an obvious downside I don't see how they can really compete at all w/ playwright without dealing with that.

2

u/shirabe1 Sep 08 '22 edited Sep 08 '22

Hello, I also work on Cypress. I don't have a lot of context on the API design (the cy and concept of a Chainable has been around forever), however, if you consider the JS ecosystem at the time the API was designed (2014~2015) async/await wasn't very widespread, and many browsers did not have built-in Promise support, either. So, considering the alternative (lots of then chains or callbacks) the Chainable API probably made a lot of sense.

I hope Cypress can work with async/await, but as a mature product, it would be irresponsible to move away from the existing public API and model in a major fashion.

Perhaps something akin to React Class API -> Function API or Vue Options API -> Composition API would be most appropriate - add a more modern take, without impacting users on the existing API, and continue to allow and support it. It seems this worked great for both React and Vue - write new code in the modern style, and keep existing code as-is. The "RIP of the bandaid" approach Angular went with definitely didn't win it any fans, although it still enjoys popularity.

1

u/qalc Sep 09 '22

I mean yeah, wouldn't it be doable to just add support for async/await. I personally wouldn't care necessarily about having legacy tests stay on the method chain train for a while, but it would be nice to start using the modern apis. The proprietary/locked-down nature of parallelization is also pretty frustrating. I feel like theres other ways of making money.

1

u/shirabe1 Sep 09 '22

Note: all my personal opinions.

I cannot speak too much to business side since I’m not very involved there. My goal is better testing tooling for the web.

I think the challenging part of adding a more “modern” API is less technical and more the challenges associated with compatibility with the wider ecosystem.

It’s also important to consider that once a product reaches a certain maturity level, introducing large API changes is not always a good move, even if the changes in isolation are better. Mature software projects should avoid large changes without a good reason.

I personally would love to see an opt-in or even alternative async/await API, as long as it doesn’t negatively impact existing tests/users. I think for a QA tool, backwards compat and reliability are the biggest priority.

Ultimately, I think we can have both!

32

u/zephyrtr Sep 08 '22

Playwright just let's you async await? Shiiiiiit. The quasi promises of Cypress are awful.

3

u/thruster_fuel69 Sep 08 '22

This was why I wrote off cypress, it's technically inferior. QE loves it though, marketing hyyyype 😤

2

u/qalc Sep 08 '22

They suck so bad dude. Like being teleported back 5 years. I'm now building any new test frameworks at my company in playwright and it's better in literally every way.

7

u/Plorntus Sep 08 '22

Going through the Cypress codebase was one of the most mind boggling experiences ever. It works arguably but my god it's bad. They have made some strides to clean it up but last I checked it was a pool of just bad decisions on top of more bad decisions with some coffee script, javascript, typescript and outdated code mixed in.

Really, its commendable they've managed to make the product they have although it'd probably benefit them if they spent a month just cleaning it up and modernising.

The Cypress cucumber plugin is also a fun one which recompiles the bundle for every single test.

3

u/FINDarkside Sep 08 '22

It's mind boggling that this has been unresolved issue for over two years. How ridiculous it is that cypress cannot handle the app re-rendering? When huge amount of users are using some SPA framework or something else to cause changes to DOM. Cypress became pretty much unusable for our tests once we enabled SSR for our react app since cypress just breaks when React tries to hydrate.

1

u/Sushrit_Lawliet Sep 08 '22

Thanks for the detailed breakdown OP!

4

u/gajus0 Sep 08 '22

Choosing Cypress for our project was one of the bigger mistakes I've made. I really fell in the trap of familiarity (was already familiar with Cypress from previous projects) and popularity (Cypress is more popular, no question). Instead, I should have talked with QA engineers who've used both.

I feel obliged to spread the gospel now.

1

u/Sushrit_Lawliet Sep 08 '22

Totally get your pain, been there before. I had cypress setup on a personal project but never got around to actually set up anything beyond jest and storybook. It just felt a bit too overkill for the project. In the midst of setting up a project for a client and was actually piecing together the stack and I’m glad I ran into this. I’ve used Playwright and Puppeteer before to build pdf and image generation lambda functions and I actually wondered about using them for this, but as you said cypress is so well marketed in this aspect that it can sometimes convince you.

4

u/gajus0 Sep 08 '22

Heads up that Playwright and @playwright/test are distinct projects. We are migrating to @playwright/test.

1

u/Sushrit_Lawliet Sep 08 '22

Yes aware of that, thanks again!

2

u/LloydAtkinson Sep 08 '22

If feel vindicated by this a lot. I've previously known one of those annoying advocates and she was insufferable.

1

u/SgtPooki Sep 10 '22

Thanks for this.

I migrated some tests to cypress while at AWS, and I found some issues regarding async/await support. Reading the cypress maintainers' reasons that they can’t do async await were huge red flags for me. I don’t remember enough about it to go into detail. Still, I remember diving into their task queuing logic and just being horrified about how uninformed and pushy the maintainers seemed to be about it.

They sell it as crazy intuitive with their test runner UI but migrating complex test cases (from selenium, puppeteer, and others) was a horrible nightmare. Writing new tests was fine, but there‘s a considerable risk of inevitable race conditions due to their obfuscation of async work.

If your company has a controlling interest in cypress, maybe it’s worth it, but I am not a fan.

1

u/Pelopida92 Sep 10 '22 edited Sep 12 '22

Great post, just wanna chime in and add a couple points.

I would argue that Playwright has even MORE backing than Cypress, considering that it's backed by Microsoft.

Then, all your points about Playwright being better are correct, but you didnt mention probably the most important one: browser support. Currently Cypress doesnt support Safari and has hacky support for Firefox. Playwright support every browser with no distinction.

2

u/[deleted] Sep 07 '22

Ditto! I like Cypress just fine.

0

u/[deleted] Sep 07 '22

[deleted]

3

u/LloydAtkinson Sep 08 '22

100%. I don't understand why they are against the POM and then don't even have a working example of what they advocate for instead (which isn't even clear or explained!). I also strongly dislike their stupid stringly-typed heavy API instead of a better DOM query library. The fact that you need to chain practically everything, for side effects, is just fucking weird.

4

u/Barbas Sep 07 '22

I can definitely see unit test ports between frameworks and languages becoming one the main applications for GTP3-like tech.

The input and output is well structured usually, tests are usually short enough that the model csn still produce good results, and correctness should be relatively easy to verify (perhaps using fuzzing too?)

1

u/Kevin_Jim Sep 08 '22

To avoid having to do things like that it’s good to use CodeceptJS. It’s a front-end that can utilize any testing/automation backend you want. Highly recommended.

1

u/John4qa Jul 17 '23

Hello Friend,
If you want to see the comparison Cypress / vs Playwright
Please follow the below links for detail comparison.
Cypress / vs Playwright