DEV Community

Cypress - End to End Testing Framework

Bushra Alam on March 07, 2020

Cypress is the new kid in the front-end testing market which is gaining popularity amongst testers and developers alike. It can test anything that ...
Collapse
 
benjaminsmiths profile image
Ben Smith

I use Cypress now for all my projects and combined with Typescript it’s the only testing I do. No unit or component testing as I find this combination catch’s the majority of issues and it’s it’s fast to develop and maintain.

Good, through run though thanks for sharing

Collapse
 
jaredmeakin profile image
Jared Meakin

For those using cypress for E2E testing do you simply mock all your API calls?

Has anyone found a testing strategy similar to how RoR does it? Specifically, a test database that gets reset after every run?

I just hate mocking every single endpoint in JS projects that heavily rely on multiple microservices. It becomes an exercise in tedium.

Collapse
 
dmitry profile image
Dmitry Polushkin • Edited

In rails you normally can use FactoryBot through middleware on server side and commands to clean/setup specs in cypress. Doing calls to your real app makes everything updated, no need to write mocks, especially it can be a problem for websocket apps (not enough tools for now to mock websocket). So in other words you are doing integration testing, which is higher level, with easiness of unit testing - which is absolutely great and fast enough.

One of the solutions: github.com/shakacode/cypress-on-rails

Collapse
 
yucer profile image
yucer

I do also like to test the middle-ware directly.

It forces you to keep all the logic away from client code.

If someone implements part of the business logic with a javascript in the browser ... that is not secure and might not end well in some cases.

My opinion is that the browser code should be used only for usability requirements. And those use cases need also test, so +1 for cypress for helping on that.

Nevertheless the middleware / APIs should be tested directly, also for preventing those invalid input cases that the interface might never send, but that might break everything.

Collapse
 
yucer profile image
yucer

Do you define all your interfaces by hand ?

In the case of REST endpoints, the same code generator (Ex: openapi-generator) that generate the endpoints from the API specs (Ex: OpenAPI) might generate the client library with basic unit tests.

Collapse
 
jaredanson profile image
Jared

I recommend using Cypress Testing Library to extend the commands cypress can use. I very rarely use cy.get and instead use the commands that Cypress Testing Library gives you.

You can do things like cy.findByPlaceholderText, cy.findByText, or cy.findByTestId for hard to grab elements. And retries are built in if I remember correctly.

If you have more than one element that matches you can just add All after the find, for example cy.findAllByText and if you want to grab the first you'd do cy.findAllByText("hello button").eq(0).click()

testing-library.com/docs/cypress-t...

Collapse
 
nutankm profile image
Nutan Kumar

Hi Bushra,
Thank you for the very good article. I am new to cypress and javascript. please help me with below code. trying to get latest string value and compare it in while loop. but in while loop it keeps picking up old string value.
Thanking you in advance. Really appreciate your help.

allOpenCases.getCurrentSystemStep().then(($currentStep)=>
{
return $currentStep.text()

}).as('CSS')

cy.get('@CSS').then((cStep)=>{
let count = 1
while(cStep!==targetSystemStep && count < 50)
{
allOpenCases.getSearchButton().click()
cy.wait(3000)

allOpenCases.getCurrentSystemStep().then(function($currentStep)
{
let cStep = $currentStep.text() //latest text

})

count ++
}

})

Collapse
 
asherirving profile image
Asher Irving

Uh, using Cypress is such a bad experience.
I tried it because a colleague heard about the hype.
All your posts seem to be related to Cypress.
Why is that?

Collapse
 
chautelly profile image
John

I just finished figuring out how use Cypress combined with XState for auto-generated model based tests. I also wanted to mock API responses a little differently than Cypress suppports out-of-the-box currently but I was able to make it work. I really like that I can review the test at each stage with Cypress. Anyways I posted about my experience here: dev.to/rjdestigter/model-based-ui-...

Collapse
 
sunnysachdeva99 profile image
sunnysachdeva99

Agree Cypress is evolving however it can be fit everywhere. Not sure but it does not provide a free hand in using all the JS capabilities like in wdio. working with nested iframes is a pain. I don't want to change/manipulate the dom just to handle the new window opened by a link.

Also, on the architecture, I am not able to launch the url hubspot.com/ in Cypress but in wdio or playwright that's not a issue

Collapse
 
albertomontalesi profile image
AlbertoM

We've been using Cypress for now one and a half year. It's very good and easy to use

Collapse
 
chema profile image
José María CL

Awesome! Thank you so much for sharing. I'm trying to implement Cypress in our React App.

There are just few things I don't get... should we use our real graphql service (in my case) to test the data or it's better to mock the graphql requests??

If we should mock the service data.. it's not complicated to maintain the tests every time the requirements changes? Thanks in advance!

Collapse
 
jonsy13 profile image
Vedant Shrotria

It was an awesome description as well as a great read through tutorial.
Thanks Bushra Alam for such a simple got through post.
Need an advise for E2E Testing, if we have a frontend and backend both.
For using Cypress on frontend ,i will be needing my backend server running as well.So, how to manage frontend,backend and Cypress in CI?
Any advise is appreciated.
Thanks Again!!

Collapse
 
jaxon profile image
Ryan J

My project is trying to get automated UI testing setup. I might try this out. Thanks for the introduction.

Collapse
 
lauriy profile image
Lauri Elias

I heard TestCafe is in vogue now.

Collapse
 
monu180 profile image
Monu Kumar

thank you for sharing nice blog

Collapse
 
codeur47 profile image
YORO Ange Carmel

Very useful article. Thank Bushra