DEV Community

Cover image for Unlocking the Power of Conditional Testing in Cypress
Atul Sharma
Atul Sharma

Posted on

Unlocking the Power of Conditional Testing in Cypress

Conditional testing in Cypress involves using conditional statements and commands to control the flow of your test scripts based on certain conditions. Cypress provides a powerful set of commands for handling conditional logic within your tests. Here are some common scenarios and techniques for conditional testing in Cypress:

1. Conditional Assertions

You can use conditional logic to make assertions based on the state of the application. For example, you might want to assert that a certain element is visible only if a specific condition is met. Here's an example:

   cy.get('.some-element').should(($el) => {
     if (condition) {
       expect($el).to.be.visible;
     } else {
       // Add assertions or actions for the opposite condition
     }
   });
Enter fullscreen mode Exit fullscreen mode

2. Using if Statements:

You can use standard JavaScript if statements in your Cypress tests to conditionally execute commands or assertions. For example:

   cy.get('.some-button').then(($button) => {
     if ($button.is(':visible')) {
       // Perform actions if the button is visible
       cy.get('.some-element').click();
     } else {
       // Perform actions for the opposite condition
     }
   });
Enter fullscreen mode Exit fullscreen mode

3. cy.wait with Condition:

You can use cy.wait with a condition to pause the test execution until the condition is met. For example, you can wait for an element to exist or for a specific network request to complete:

   cy.waitUntil(() => cy.get('.some-element').should('exist'));
Enter fullscreen mode Exit fullscreen mode

4. Custom Cypress Commands:

You can create custom Cypress commands to encapsulate conditional logic and make your tests more readable. For example, you can create a custom command to log in if the user is not already logged in:

   Cypress.Commands.add('loginIfNeeded', () => {
     cy.get('.user-profile').then(($userProfile) => {
       if (!$userProfile.length) {
         // Log in logic here
       }
     });
   });

   // Usage
   cy.loginIfNeeded();
Enter fullscreen mode Exit fullscreen mode

5. Using .then() with Promises:

Cypress commands return Promises, so you can use .then() to handle conditions based on the resolved values of those promises. For example, you can check the text of an element:

   cy.get('.status-message').invoke('text').then((text) => {
     if (text === 'Success') {
       // Handle success condition
     } else {
       // Handle other conditions
     }
   });
Enter fullscreen mode Exit fullscreen mode

Please keep it in the mind when using then/should. Utilizing .then() enables you to employ the returned object in a callback function, making it suitable for scenarios where you require value manipulation or the execution of specific actions.

Conversely, when employing a callback function with .should() or .and(), a unique mechanism is in place to repeatedly execute the callback until no assertions result in errors. It's important to exercise caution regarding any side effects within a .should() or .and() callback, as these may unintentionally occur multiple times.

Remember that Cypress tests are written in JavaScript, so you have the flexibility to use all JavaScript language features and libraries to handle conditional testing. Make sure to structure your tests in a way that makes them clear and maintainable, especially when dealing with complex conditional logic.

Catch you in the next article coming next week. Feel free to comment on this.

Please refer to my online portfolios:
LinkedIn
GitHub
StackOverflow

Top comments (0)