Test automation is critical for delivering excellence in a shorter release cycle in the software development process. As a result, selecting the right Test Automation framework, such as Selenium or Cypress cloud, becomes critical for the process. Selenium vs Cypress can help you determine when to use which Automation framework to meet all of your testing needs.
What Is Cypress?
According to the official documentation, Cypress differs architecturally from Selenium.
QAs and developers can use Cypress to create:
- Integration tests Unit tests
- Tests from beginning to end
The architectural enhancements in Cypress enable testers to perform Test-Driven Development (TDD) with complete end-to-end testing. Cypress was created intending to parallelize development and testing.
Cypress scripts, unlike Selenium scripts, cannot be executed outside of the browser. Cypress test scripts are all run in the browser. For example, to click a specific button, Cypress does not use a specific driver to send the command to the browser. Instead, it sends the click command to the button via DOM events. As a result, test results are executed much more quickly.
The following are two key features designed specifically for the Cypress framework:
- Automatic waiting – Cypress waits for elements to become visible, animations to finish, the DOM to load, XHR and AJAX calls to complete, and so on. As a result, there is no need to define implicit and explicit waits.
- Real-Time Reloads – Cypress is smart enough to recognize that after saving a test file (xyz spec.js), the tester will run it again. As a result, when the tester saves their file, it automatically starts the run next to the browser. As a result, there is no need to manually start the run.
Some features that make Cypress an appealing option for automated testing are:
- Cypress can be installed simply by using npm or Yarn. There are no other dependencies to install.
- Clear API: Cypress’s API is simple and easy to understand. All the commands are chainable, making the code simple to read and test.
- Waiting automatically: Cypress waits for elements to appear before performing any actions on them. This improves the code’s reliability and makes it easier to write.
- Parallel execution: Cypress Test Runner allows you to run tests on multiple browsers and devices at the same time. This speeds up the execution of tests.
Advantages Of Cypress
- The Cypress framework takes snapshots during test execution. This enables QAs or developers to hover their mouse over a specific command in the Command Log to see exactly what happened at that point.
- Unlike Selenium, there is no need to include explicit or implicit wait commands in test scripts.
- Developers or QAs can use spies, Stubs, and Clocks to verify and control the behaviour of server responses, functions, or timers.
- Before performing any action, the automatic scrolling operation ensures that an element is in view (for example Clicking on a button)
- Previously, Cypress only supported Chrome testing. Cypress now supports the Firefox and Edge browsers, thanks to recent updates.
- Cypress executes commands in real time as the programmer writes them, providing visual feedback as they run.
- Cypress comes with extensive documentation.
Limitations Of Cypress
- Cypress cannot be used to power two browsers at the same time.
- It does not support multiple tabs.
- Cypress currently does not support browsers such as Safari and Internet Explorer.
- Support for iFrames is limited.
How Do You Get Started With Your New Cypress Testing Cloud?
Step 1: Launch your server
Assuming you’ve successfully installed Cypress and opened it in your project, the first thing you’ll want to do is launch the application’s local development server.
Step 2: Go to your server
- It’s time to pay your server a visit now that it’s up and running.
- Now, let’s make our own spec file, home page.cy.js.
- Once that file has been created, it should appear in the list of spec files.
- Now, double-click the home page.cy.js file to see Cypress launch your browser.
- You will see the error if you do not start your server.
- If you’ve started your server, your application should be loaded and operational.
Step 3: Set up Cypress
If you think ahead, you’ll quickly realize that you’ll be typing this URL a lot, because every test will require a visit to some page of your application. Fortunately, Cypress has a configuration option for this. Let’s capitalize on that right now.
Open the configuration file. It’s empty at first, but let’s add the baseUrl option.
This will automatically prefix commands like cy.visit() and cy.request() with this baseUrl.
When you change your configuration file, Cypress will reboot and close any open browsers. This is typical. To relaunch the browser, click on the spec file again.
We can now visit a relative path without specifying the hostname or port.
Strategies For Testing
You’re about to write tests for your application, and since only you know your application, we can’t give you much specific advice.
It is entirely up to you, your application, and your team to decide what to test, where the edge cases and seams are, and what regressions you are likely to encounter.
However, modern web testing has a few kinks that every team encounters, so here are some quick tips on common scenarios you’re likely to encounter.
Depending on how your application is built, your web application will most likely be affected and controlled by the server.
If your server is running node.js, you could add a before or beforeEach hook that executes an npm task. Instead of simply executing a system command, you may prefer more flexibility and expose a series of routes only when running in a test environment.
You could, for example, combine several requests to tell your server exactly what state you want to create. While there is nothing wrong with this approach, it adds a significant amount of complexity. You’ll have to deal with synchronizing the state of your server and browser – and you’ll always have to set up / tear down this state before tests (which is slow).
Creating Your First E2E Exam
Include a test file
Assuming you’ve successfully installed and launched Cypress, it’s now time to add your first test. You can do this by clicking the Create new empty spec button.
When you click it, see a dialogue where you can name your new specification. Accept the default name for the time being. A confirmation dialogue displays the newly generated specification. Simply close it by pressing the button.
After you’ve created that file, it should appear immediately in the list of end-to-end specs. Cypress monitors your spec files for changes and displays them automatically.
Cypress displays the spec list, including the newly created spec.
Even though we haven’t yet written any code – which is fine – let’s click on your new spec and watch Cypress launch it. Spoiler alert: it will most likely FAIL. Don’t worry, this is simply because you haven’t yet configured Cypress to visit a page in your app! Let us try something new.
Make your first test
It’s now time to write your first test. We’ll do the following:
- Create your first passable test.
- Change it so that it fails.
- Cypress reloads in real time.
Open your preferred IDE and replace the code with the contents of your specification.
When you save this change, the browser should reload.
Notice: On the right, Cypress displays a message showing that this is the default page. Cypress assumes you’ll want to visit a URL on the internet, but it can also function without it.
Let’s write the first failing test now. When you save again, Cypress will highlight the failing a test in red because true does not equal false.
In addition, Cypress displays the stack trace and the code frame where the assertion failed (when available). You can use your preferred file opener to open the file where the error occurred by clicking on the blue file link.
Cypress provides a visual representation of suites, tests, and assertions. Commands, page events, network requests, and other features will be added soon.
Create an accurate test
A solid test typically comprises three stages:
- Configure the application state.
- Take some action.
- Assume something about the resulting application state.
- This is also known as “Given, When, Then,” or “Arrange, Act, Assert.” But you first put the application in a specific state, then you do something in the application that causes it to change, and then you check the resulting application state.
Today, we’ll inspect these steps and map them to Cypress commands:
- Go to a website.
- Look for an element.
- Engage with that element.
- Make a claim about the page’s content.
Step 1: Go to a specific page
Let’s start with a web page. In this example, you will visit our Kitchen Sink application so that you can try Cypress without having to worry about finding a page to test.
You can use cy.visit to send the URL we want to visit (). Let’s replace the previous test with one that actually goes to a website:
Return to the Cypress Test Runner after saving the file. You may notice the following:
- The new VISIT action is now visible in the Command Log.
- The App Preview pane now displays the Kitchen Sink application.
- Although we made no assertions, the test is green.
- Until the page finishes loading, the VISIT displays a blue pending state.
Step 2: Look for an element
Now that you’ve loaded a page, we need to do something with it. You should use cy.contains to find this element based on its contents ().
Let’s include it in the experiment and see what happens.
This test should now show CONTAINS in the Command Log while remaining green.
Even without an assertion, you know everything is fine! This is because many Cypress commands fail if they don’t find what they’re looking for. This is referred to as a Default Assertion.
To test this, replace type with something that isn’t on the page, such as hype. The test will turn red, but only after about 4 seconds!
Can you see what Cypress is doing behind the scenes? It is automatically waiting and retrying because it expects the content to be found in the DOM at some point. It does not fail right away!
Let’s get this test back to passing before we add another command. Substitute type for hype.
Step 3: Select an element by clicking it
Now we’re going to click on the link we discovered. How do we go about it? Add a.click() command after the previous command,
It almost reads like a short story! Cypress refers to this as “chaining,” and we chain commands together to create tests that truly express what the app does in a declarative manner.
Also, the App Preview pane has been updated further after the click, following the link and displaying the destination page:
Now we can make a claim about this new page!
Step 4: Make a claim
Let’s make a claim about something on the new page we’ve arrived at. Perhaps we should double-check that the new URL is the expected URL. We can accomplish this by looking up the URL and chaining an assertion with.should to it ().
Increasing the number of commands and assertions
In a test, we are not limited to a single interaction and assertion. In fact, many interactions in an application may require multiple steps and will most likely change the state of your application in over one way.
You can extend the interactions and assertions in this test by adding another chain that interacts with and validates the behavior of elements on this new page.
To select an element based on its class, we can use cy.get(). Then, using the.type() command, we can enter text into the selected input. Finally, we can ensure that the value of the input corresponds to the text typed with another.should ().
And there you have it: a quick Cypress test that visits a page finds and clicks a link, validates the URL, and then validates the behavior of a new page element. If we read it aloud, it could sound like this:
- Go to https://example.cypress.io/.
- Locate the element that contains content: type
- Simply click on it.
- Assert that the URL includes: /commands/actions
- With the action-email class, you can get the input.
- Fill in the blanks with email@example.com.
- Assert that the input now reflects the new value.
Cypress Test Suite Upgrade
When upgrading from one version of Cypress to another, it is critical to be aware of any breaking changes. These changes may cause your Cypress tests to fail, and you may need to update your code in order to continue using the new version of Cypress.
Transitioning From Selenium
If you’re currently testing web applications with Selenium, think about switching to Cypress. There are several reasons you should do this:
- Cypress’s API is simpler and more user-friendly than Selenium’s.
- Cypress tests are executed in the same environment as the application under test, making debugging easier.
- Cypress can wait for elements in the DOM to appear before interacting with them, making tests more robust.
WebDriverIO and Protractor are the two main options for migrating from Selenium to Cypress.
WebDriverIO is a Node.js library that wraps the Selenium WebDriver API in a protective shell. It has an API that is like Cypress, making it simple to migrate existing Selenium tests to WebDriverIO. How to do this is documented on the WebdriverIO website.
Protractor is an end-to-end testing framework for Angular applications that makes use of the Selenium WebDriver API. Its architecture is like that of Cypress, making it simple to migrate existing Selenium tests to Protractor. How to do this is documented on the Protractor website.
The cloud-based testing platform like LambdaTest offers end-to-end testing on a fast Cypress test execution cloud. It is a reliable, scalable, and secure test execution platform built for scale. You can test your Cypress test scripts on 50+ different browser versions.
Cypress and other automated testing tools can make our lives as developers much easier. We can quickly detect errors and avoid potential regressions by running tests automatically.
While it may tempt to rely solely on automated tests, manual testing is still necessary. Automated tests can detect some bugs, but they cannot detect all of them. A human eye is still required to detect typos, layout issues, and user experience issues.
Cypress is a modern, open-source test suite for web applications. It’s built on top of Electron, which is used by the Atom text editor and many other applications. Cypress is easy to install and has a simple, clear API.
Read more: How to improve your blog