The series "Pinches of Cypress" is getting track!
Today you will learn how to ensure that your tests behave the same way when run multiple times.
Cypress.io is a fantastic testing framework!
It packs together with it several other famous libraries to make life easier for its users, that is, your life!
One of these libraries is lodash.
Just use
Cypress._
And you can invoke any feature of lodash. 🤩
Among the various features of lodash, one that “fits like a glove” for the problem we want to solve is .times.
An example would ease the understanding, right?
Here we go!
Cypress._.times(5, () => {
it.only("successfully submits the form", () => {
const customer = {
firstName: "John",
lastName: "Doe",
email: "john-doe@example.com"
};
cy.fillMandatoryFields(customer);
cy.contains("Send").click();
cy.get(".success p")
.should("contain", "Form successfully submitted.");
});
});
After running the above test, I have the following result.
Sample app
âś“ successfully submits the form (1725ms)
âś“ successfully submits the form (1212ms)
âś“ successfully submits the form (1200ms)
âś“ successfully submits the form (1195ms)
âś“ successfully submits the form (1256ms)
5 passing (8s)
As you can see, my test looks stable and passed 5 times in a row. Yay! 🎉
Problem solved!
What's up? Any thoughts about the series?
I'm collecting feedback and creating a backlog for the next posts. Leave a comment with what you would like me to address in future content.
This post was originally published in Portuguese on the Talking About Testing blog.
Would you like to learn about test automation with Cypress? Get to know my online courses on Udemy.
Top comments (8)
What an elegant solution!
Thanks!
@walmyrlimaesilv ..thanks for the nice post.
How is it different from 'retries' in cypress?
The idea of using it would be to test that your test is stable, not to retry in case it’s flakey.
Got it. Thanks @walmyrlimaesilv
You’re welcome!
Nice and easy, will be using this on my tests. Beats having to manually reload cypress test runner to re-execute a test.
I’m glad you liked it!