In this blog we deep-dive into Cypress Automation Framework, a JavaScript-based end-to-end testing framework that can be used for automating web apps.
Cypress test automation is one of the JavaScript-based end-to-end testing frameworks that can be used for automating web apps. Cypress provides an all-around solution for creating, executing, and debugging tests that engage with the user interface (UI) of online applications.
Cypress’s Architecture
Features
- JavaScript-Based: Because Cypress is based on JavaScript, developers who are already familiar with JavaScript frameworks or libraries can use it right away.
- End-to-End Testing: Cypress focuses on end-to-end testing, which replicates actual user interactions with the program at various application stack layers, including the user interface (UI), backend APIs, and databases.
- Test Runner and Test Execution: Cypress includes a test runner that enables you to run tests and inspect the results of those tests. It gives a visual representation of the program being tested and has capabilities like live reloading, snapshots, and time-travel debugging.
- Automatic Waiting and Retries: Cypress manages asynchronous operations and automatically waits for DOM elements to become accessible, eradicating the need for explicit waits or timeouts. Additionally, it contains built-in retry techniques to deal with erratic tests.
- Interactive Test Development: Cypress offers a platform that enables developers to write tests in real time and view the results right away in a browser. This makes testing during the development process simple to debug and troubleshoot.
- Real-Time Reloads: Cypress automatically reloads and reruns the tests as you make changes to your test code, letting you see the results of your modifications right away.
- Time-Travel Debugging: Cypress has a special feature called time-travel debugging that enables you to pause the test execution at any time and view the status of the application. Effective problem understanding and diagnosis are aided by this.
- Network Stubbing and Spying: Cypress gives you the ability to stub or intercept network requests so you may mimic various server responses or run test scenarios. In order to assert and validate API interactions, it also offers the capability of spying on network requests.
- Cross-Browser Testing: Testing across multiple browsers is supported by Cypress, including Chrome, Firefox, and Electron. It guarantees consistency in test execution across several browsers, ensuring that your application functions properly in various settings.
- Extensibility: Cypress is extensible through plugins, which let us add customized functionality, integrate it with other tools, or improve the testing capabilities in accordance with your needs.
- Headless Mode: Cypress can be executed in headless mode, allowing tests to run without a graphical user interface. This is useful for running tests in environments without a display server or for incorporating Cypress into continuous integration pipelines.
- Video Recording: Cypress has the capability to record videos of test runs. This feature is valuable for debugging purposes, as it allows developers to visually inspect the test execution and identify issues more easily.
- Parallel Test Execution Support: Cypress supports parallel test execution, enabling faster test runs by distributing tests across multiple instances or environments simultaneously. This is particularly beneficial for large test suites and helps in optimizing testing efficiency.
- Community Support: Cypress has a vibrant and active community of developers and testers. The community support includes forums, online discussions, and resources where users can seek help, share experiences, and contribute to the improvement of Cypress. This collaborative environment enhances the tool's usability and promotes knowledge sharing.
Setting Up Tests
- Cypress Installation: Begin by setting up Cypress on your local computer. Cypress can be installed either globally or locally as a project requirement using npm (Node Package Manager).
npm install cypress - Create a new Cypress project by using the Cypress CLI (Command Line Interface) command to create one in the directory of your choice.
npx cypress open
- Configuring Cypress: After the project is launched, Cypress will generate a folder structure with predefined settings. The configuration files (cypress.json, cypress/plugins/index.js, and cypress/support/index.js) can be modified to alter Cypress' behavior to suit the needs of your project.
- Write Test Scripts: In the Cypress Test Runner, JavaScript is used to create and execute Cypress tests. By default, Cypress offers a sample test script (sample_spec.js in cypress/integration/examples). To write your own test cases, you can edit this file or make new test files in the cypress/integration directory.
describe('Test Suite Name', () => {
it('Test Case Name', () => {
// Test steps and assertions
});
});
- Execute the Tests: The Cypress Test Runner can be used to execute the tests after your test scripts are in place. To run tests, monitor test results, and debug test cases, the Test Runner offers a graphical interface.
Run the following command in the directory for your project:
npx cypress open
- Select Tests to Run: The Cypress Test Runner will launch and show a list of test files. You have the option of running all tests or just the one you want. A test file's related test case(s) will be run when clicked, and the Test Runner will show the results of the live test execution.
- Analyze Test Results: In the Test Runner interface, you can evaluate the test results after the test execution is over. It offers thorough logs, images, and videos of the test run, making it simple to examine the results and spot any errors or problems.
- Automate Test Execution: You may incorporate Cypress into your continuous integration (CI) or continuous delivery (CD) pipelines to automate the execution of Cypress tests. As soon as code changes are submitted to the repository, this enables tests to be run automatically. Cypress has several CLI commands (such as "cypress run," "cypress run --spec," etc.) that can be used to run tests in headless mode or in other configurations appropriate for automation.
Cypress Folder Structure
- Fixtures: The test data in this folder is in the form of JSON files or key-value pairs. During test execution, fixtures can be used to provide data to test cases.
- Integration: You should keep your test cases or specs in this folder. Depending on the different aspects or functionalities of the program, test cases are arranged into subdirectories.
- Package.json: The package.json file serves as the project's configuration file. It includes dependencies, scripts, and project metadata. It is used to develop custom scripts for things like test execution, build processes, managing project dependencies, and more.
- Plugins: The plugins folder contains JavaScript files that specify different events and actions to be carried out prior to or following test runs. These occurrences can be used to enhance Cypress' functionality or alter how tests are run.
- Node_modules: Dependencies for the project are installed in this subdirectory using npm (Node Package Manager). Cypress and other project-specific libraries can't operate properly without these dependencies.
- Support: Reusable techniques, unique commands, or utilities that can be directly utilized by test cases are stored in the support folder. Common setup and teardown logic is typically defined here, as well as helper functions.
- Videos: When enabled, Cypress can record videos of test execution. The recorded test steps are kept in video format in the movies folder for subsequent review or analysis.
- Cypress.json: The configuration for Cypress is stored in the cypress.json file. You can change the default configurations or specify your own configurations specifically for your project. It contains options such as base URLs, environment variables, and test file patterns, among others.
How to Write Tests
Determine the functionality or elements of the web app that need to be tested before creating the test scenarios. Based on user interactions, expected outcomes and any needs, differentiate between the various test scenarios.
Prepare a fresh test file: Navigate to the cypress/integration directory in the Cypress project tree. Depending on your test scenario, make a new test file (like test_trial_spec.js) or alter an existing one.
Create test cases by using the description and its functions in the test file to specify test suites and specific test cases, respectively. Include the test cases' assertions and test processes in your writing.
describe('Test Suite Name', () => {
it('Test Case Name', () => {
// Test steps and assertions
});
});
Use Cypress commands: To interact with web elements, carry out activities, and make assertions, Cypress offers an extensive range of built-in commands. Utilize these instructions to assert expected behaviors, validate responses, and observe user activities.
- cy.visit(url): Loads the URL of the requested web page.
- cy.get(selector): Locates a page element using a CSS selector.
- cy.contains(content): Finds a containing element for the given text.
- cy.click(): Clicks on an element.
- cy.type(text): Enters text in a field.
- cy.clear(): Clears the value of an input field.
- cy.should('be.visible'): Indicates that an element is visible on the page.
- cy.should('have.value', expectedValue): Asserts that an entry field has the expected value.
- cy.should('have.length', expectedLength): Counts the number of elements that match a selector.
- cy.contains(content).click(): Locates and clicks on an element that contains a particular text string.
- cy.wait(time): Pauses the running of the test for the provided time.
- cy.url(): Retrieves the current URL of the page.
- cy.get('[data-cy="element"]').click(): Locates an element using a data attribute and clicks on it.
- cy.reload(): Reloads the current page.
Utilize test hooks and utilities: Cypress offers hooks like before, beforeEach, after, and afterEach that let you set up prerequisites or carry out cleaning operations prior to or following test cases or test suites. In order to reuse common test steps across several test cases, you may also develop your own utility functions or aliases.
Execute the tests: Use the command npx cypress open to launch the Cypress Test Runner. Launching the Test Runner interface will show a list of test files. The tests will run in the browser window that Cypress has opened when you click on the test file or test suite you want to run.
Debug and troubleshoot: Make use of Cypress' robust debugging tools to resolve any problems or test failures. To comprehend how your tests behave, you can add breakpoints, examine the state of the application, and take advantage of interactive capabilities in the Cypress Test Runner.
Analyze test results: Examine the test results in the Cypress Test Runner when the test run is finished. Examine the screenshots or videos that were taken throughout the test run to better understand the context of any failed assertions or faults.
Refactor and maintain tests: You might need to update and refactor your tests as your application changes. Keep your test code organized, maintainable, and modular. Use Cypress' best practices and suggested patterns to make sure your test suite is robust and scalable.
How to Run Tests
Ensure that Cypress is completely installed on your computer. Run the following command to install it using npm (Node Package Manager):
Npm install -g cypress
The root directory of your Cypress project can be found by opening a terminal or command prompt.
By executing the following command, the Cypress Test Runner will be launched:
Cypress open
The Cypress Test Runner GUI will open once you issue this command.
- A list of test files can be seen on the left side of the Cypress Test Runner. To execute the tests found in a test file, click on it.
- The Cypress Test Runner will launch a new browser tab after you choose a test file, at which point your tests will begin to execute. The Cypress Test Runner interface allows you to track the status of the test execution.
- You can influence the test execution by halting, starting, or running tests again. The Test Runner interface also allows you to view the test results, along with any assertions or problems.
- You can evaluate the test results, any generated reports, and screenshots in the Cypress Test Runner interface when the test execution is over.
Debugging Tests
Examine Test Failure: If a test fails, investigate the stack trace or error message that Cypress has provided. This will show you where the issue is coming from.
Add Statements: To output pertinent information to the Cypress Command Log or browser console, include logging statements using the cy.log() or console.log() functions within your test code.
Pause Test Execution: To halt the test execution at a certain point, use the cy.pause() command. As a result, you can interact with the Cypress Test Runner to view the state of the application, perform debugging using the developer tools of the browser, or manually check elements or data.
Examine Elements: To examine an element's status, use the Cypress cy.get() command in combination with.debug(). In order to make sure the right element is targeted, this will report the element's properties and attributes to the Cypress Command Log.
Isolate the Problem: If the test fails at a particular test step or assertion, try using.only() to execute just that step or assertion and temporarily commenting out other test code.
Take Screenshots or Videos: To take screenshots or record videos of the test execution, use Cypress methods like cy.screenshot() or cy.recordVideo().
Examine the test environment: To make sure that everything is configured properly, including the application being tested, any necessary dependencies, and test data.
Examine Test Code: Check your test code for logical mistakes, bad selections, missing or erroneous assertions, and improper test configuration.
Utilize browser developer tools (such as Chrome DevTools) to debug JavaScript code, inspect network queries, and look at element properties. By selecting the browser's icon, you can open the developer tools from the Cypress Test Runner.
Different Types of Tests
Unit Testing: In Cypress, unit tests can be created to evaluate specific operations, procedures, or application elements. In order to isolate the unit under test, Cypress offers the option to stub or mock dependencies.
End-to-End (E2E) Testing: Cypress is a good choice for end-to-end testing, which involves testing the full application in a real-world setting. The entire user flow across several pages or components can be tested using E2E tests, which replicate user interactions.
Integration Testing: Cypress makes integration testing possible by replicating the interactions between various application modules or components. It can verify how well various system components integrate and communicate.
API Testing: API testing is carried out by Cypress by sending HTTP queries and checking the returned results. It can vouch for the accuracy of API endpoints, status codes in responses, data integrity, and other API-related standards.
Regression Testing: Cypress can be used for regression testing to make sure that new modifications don't break any already-existing features and functionalities. To verify that previously tested scenarios continue to function as intended, regression tests can be automated and conducted.
Visual Regression Testing: Cypress enables visual regression testing, which uses screenshots or visual comparisons to find any visual discrepancies between the anticipated and real application user interfaces. This can aid in detecting unintentional UI changes.
Accessibility Testing: Testing for Accessibility: Cypress is a tool that may be used to evaluate an application's accessibility for users with impairments. It could run tests to verify adherence to accessibility standards and guidelines.
Performance Testing: Cypress can be expanded to conduct performance testing even though it was primarily created for functional testing. Cypress can assist in locating performance bottlenecks by monitoring response times, network requests, or other performance indicators.
Continuous Integration (CI) Testing: Cypress can easily integrate with CI/CD pipelines, enabling tests to be run immediately on each time a commit is made to a piece of code. Continuous integration testing is made possible, ensuring that changes do not introduce regressions.
Data-Driven Testing: Cypress enables the execution of tests using a variety of datasets or test data inputs. This makes it possible to test different situations and edge cases without having to duplicate test code.
Who Makes Use of Cypress?
Developers: Cypress is well-liked by developers because it offers a JavaScript-based testing framework, and it gives programmers the ability to test their code's functionality and quality using unit, integration, and end-to-end testing.
QA/Testers: Cypress has an intuitive user interface, strong debugging tools, and real-time reloading, all of which make writing and maintaining test cases simpler. In order to validate application behavior, find defects, and ensure the overall quality of the software.
Agile/DevOps Teams: Cypress works well in setups that use continuous integration/continuous delivery (CI/CD) pipelines and frequent releases, which are characteristics of Agile and DevOps teams. In order to automate testing and deliver speedy feedback on the caliber of software modifications, it can be incorporated into CI/CD workflows. Cypress is effective for teams working on numerous projects or features at once because it also offers parallel test running.
Startups & Small Enterprises: Due to its open-source nature, simplicity, and ease of deployment, Cypress is frequently used by startups and small enterprises.
Large businesses and organizations with intricate online applications frequently use Cypress for their testing requirements. Testing enterprise-level apps is a good fit for it due to its solid features, scalability, and compatibility with contemporary JavaScript frameworks.
Cypress Vs. Selenium
Cypress | Selenium | |
Testing | Front end and APIs, end-to-end. | End-to-end, not suitable for API testing. |
Supported Images | JavaScript/Typescript. | Multiple languages are supported, such as Java, JavaScript, Perl, PHP, Python, Ruby, C#, etc. |
Audience | Developers as well as testers. | Automation engineers, testers. |
Integrations & Plugins | It has limited integrations but a rich set of plugins. | It integrates with CI, CD, visual testing, cloud vendors, and reporting tools. |
Speed | It is quicker since it has a different architecture that does not use a web driver. | Its architecture makes it difficult to develop straightforward, rapid tests. Though the platform itself is quick, you can run a lot of tests in parallel, at scale, and across different browsers. |
Ease of Use | It will be simple if you are familiar with JavaScript. Otherwise, it can be a little difficult. It is developer-friendly since developers were considered throughout the design. Additionally, it features a useful function called "travel back in time." |
People can rapidly start creating tests because it supports different languages, but it takes longer than Cypress because you need to learn syntax. |
Ease of Setup |
Run the following command immediately: npm install cypress --save-dev Unlike Selenium, it doesn't need to install any other components (web drivers). Each item is packaged. |
Since a web driver and two-component bindings are present. It takes more effort and time to install. |
Supported Browsers | All Chromium-based browsers (Chrome, Edge, Brave) and Firefox. | All browsers: Chrome, Opera, Firefox, Edge, Internet Explorer, etc. |
Disadvantages
Limited Language Support: Because Cypress is mostly based on JavaScript, teams proficient in other programming languages might need to learn JavaScript in order to use Cypress efficiently.
Tabs/Child Window Management: Cypress has restrictions when it comes to managing many tabs or child windows. When running tests, handling situations involving tabs or child windows frequently calls for workarounds.
Lack of Support for Mobile Testing: Cypress was created primarily for the testing of online applications; it does not come with built-in support for testing mobile apps. Using additional frameworks or tools in addition to Cypress may be necessary for teams seeking a product that covers both online and mobile testing.
Limited Access to the Shadow Document Object Model (DOM): Cypress does not offer direct access to the Shadow DOM. Although it can interact with elements within the Shadow DOM, it is unable to directly access or change any of its internal workings.
Limited Community Support: Cypress has a smaller and more recent community than more well-known testing frameworks. This implies that it might be harder to locate comprehensive resources, plugins, or solutions for issues.
In conclusion, Cypress is a powerful tool for testing web applications, offering robust support. Its modern approach to testing, coupled with its seamless integration capabilities, makes it an excellent choice for teams looking to enhance their testing processes.
To fully understand how Cypress can be integrated into your testing framework and to explore more of its benefits, please reach out to marketing.valorem@reply.com for more information. We are here to help you make the most of your testing efforts and ensure your team's success.