Cypress is an open-source framework for creating end-to-end tests and component tests for web applications. It runs on Node.js, which means that the code for our tests is written in JavaScript (JS). The tool offers a rich array of possibilities for creating automated tests. By using JS, we can write our tests in a simple, short and legible form, using one of the most popular programming languages in the world.
Cypress also has a built-in tool for debugging tests in real time. While the test is running, the debugger shows each phase of the test is currently running and what the result is. When the test is finished, we can go to any step and review what happened there. This provides both incredible convenience and freedom in writing tests. It also allows us to change the test code and check the results in real time.
After creating a new project, Cypress will automatically add suggested folder structure. By default, it will create:
The Cypress stack uses some popular test libraries for assertion or report generation. These include:
There are also several plug-ins that are not built-in, but they are worth installing:
Cypress is undoubtedly a great tool for testing the front-end; however, it is not without flaws.
Working with iframes is indeed difficult. Cypress has many more problems than Selenium. We have the option of using additional libraries to help work with iframes, such as "cypress-iframe", but these solutions do not always work properly.
This is not a disadvantage for everyone. I put it as a disadvantage because Selenium WebDriver supports virtually all programming languages. Another thing worth mentioning is that JS is often a more difficult programming language for beginners.
The test interface, borrowed from Mocha, provides describe(), context(), it() and specify().
context() is identical to describe() and specify() is identical to it(), so any terminology will work.
Cypress also provides hooks (borrowed from Mocha). These are helpful to set conditions that we want to run before a set of tests or before each test. They're also helpful to clean up conditions after a set of tests or after each test.
To run a specified suite or test, add .only() to the function. All nested suites will also be executed. This gives us the ability to run one test at a time and is the recommended way to write a test suite.
To skip a specified suite or test, add .skip() to the function. All nested suites will also be skipped.
Cypress automatically waits until an element reaches the state we expect. This isn’t so obvious in the world of automated testing.
In cases of assertions (for example .should('be.disabled')) Cypress will automatically wait for a certain amount of time until our element reaches the expected state, so we don't have to worry about specifying precisely when the element should meet the set requirement. Many functions have built-in implicit requirements, for example .get() and .find() expect an element to exist in the DOM, .type() expects an element to allow typing, and .click() an element to interact with. The above requirements are covered by automatic waiting, for example, when using the .click() function, Cypress makes sure that an element can be interacted with. If not, it will wait until the element: is not hidden, is not covered, is not disabled, is not under animation.
With all this acquired knowledge, it’s time to write the first, simple test. We will be using an example app used to showcase Cypress.io testing.
In this test, we checked the correctness of a predefined URL when we clicked on a link that takes us to a new page, and we also checked that the element expected on that page is visible.
In today's article, we introduced what Cypress is and its advantages and disadvantages. We encourage you to check this tool out for yourself. If you are thinking of using Cypress commercially, pay attention to the disadvantages specific to the framework, especially if you have an application that uses iframes a lot. It's worth checking whether Cypress can handle this situation. If, on the other hand, you think you don't have a need for a new tool in your project, then consider thinking about learning the tool in order to expand your skills. In other cases, we recommend giving it a shot in your web projects.