Elevate Your Cypress Tests: A Guide to Custom Commands and Environments

Normia Pop
Stackademic
Published in
4 min readFeb 5, 2024

--

Illustration made by Tania Miresan Art

Get ready to enhance your Cypress tests by using Cypress environments and custom commands. I’m Normia, and in this article, I’ll break down these concepts and share some simple examples to make your automation tests more scalable.

1. Cypress Environments: Safeguarding Sensitive Data

Cypress environments play a key role in enhancing the security and scalability of your tests. By utilizing the Cypress.env function, we can easily gain access to the values of environment variables. Typically, this feature is employed to store confidential information like emails, passwords, and tokens that shouldn’t be shared openly within the project.

How to use Cypress Environments:

To begin, consider adopting a practice where you create two separate files:

a. cypress.env.example.json

Use this file to provide examples of the data used within your tests.

b. cypress.env.json

This file serves as the main file for your environment variants. Ensure that it is listed in the .gitignore file to prevent sensitive data from being inadvertently merged. A sample structure may look like this.

By maintaining this separation, you’ll always have a clear reference for the data required to run your tests successfully. Also, from here you can easily switch between different environments, for example QA env or Prod env.

Note: Values in here will overwrite conflicting environment variables in your Cypress configuration.

2. Custom Commands: Simplifying Test Flows

Custom commands within Cypress enhance the testing framework’s functionality, enabling the creation of specific actions and assertions tailored to our needs. These commands help streamline testing processes by reducing repetition and increasing efficiency.

The Power of Custom Commands — Example of a Login Function

One of the most impactful custom commands is the ‘login’ command. This can be implemented in two ways: through the UI or via API.

Examples:

a. Login via UI

b. Login via API

The API-based login option is particularly advantageous due to its speed, making it a preferred choice for efficient testing.

Now, you can effortlessly import the custom command into any of your Cypress tests as follows:

That’s it!

Note: I I advocate for using Cypress with TypeScript, and if you choose to do so, ensure that you also declare your custom commands in the Cypress support file (index.ts). This is important to ensure that the TypeScript compiler recognizes them. Example:

Integrating Cypress Environments with Custom Commands

As we can see in the examples above, we seamlessly integrated Cypress.env, as discussed in the first part of the article. This ensures that sensitive data remains shielded from direct exposure in commands. Any alterations can be effortlessly made in the Cypress.env file, eliminating the need for modifications in tests or commands, which will enhance scalability.

Conclusion

In conclusion, implementing Cypress environments and custom commands in your Cypress tests can significantly boost efficiency, security, and scalability. By adopting these practices, you’ll find yourself spending less time tweaking and fixing failing tests, allowing you to focus on what truly matters — building reliable automation suites.

I hope you find these insights helpful on your journey to optimizing Cypress tests. Thanks for reading!

If you’re looking for tips or have questions about Cypress, I’m here to help. Visit me at https://normia.dev/ and let’s talk. I’m excited to connect and support your testing journey.

Stackademic

Thank you for reading until the end. Before you go:

--

--