How to Check for an Element that May Not Exist Using Cypress

How to Check for an Element that May Not Exist Using Cypress

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When writing automated tests with Cypress, you may encounter situations where an element might not always be present on the page. This can lead to test failures if not handled properly. In this guide, we will explore a robust method to check for the existence of an element without causing test failures.

Scenario

Consider a scenario where you are writing a Cypress test to log in to a website. The login process is straightforward, but occasionally a warning dialog appears that has to be dismissed before proceeding. The challenge is to check for this warning dialog and handle it appropriately if it exists.

Solution

To address this challenge, we employ element polling to check for the dialog within a specified period. This ensures that the test does not fail if the element is not found immediately.

Implementation

cy.get('#login-username').type('username');
cy.get('#login-password').type('password{enter}');

const ifElementExists = (selector, attempt = 0) => {
  if (attempt === 100) return null; // no appearance, return null
  if (Cypress.$(selector).length === 0) {
    cy.wait(100, { log: false }); // wait in small chunks
    ifElementExists(selector, ++attempt); // try again
  }
  return cy.get(selector, { log: false }); // done, exit with the element
};

ifElementExists('.warning').then($el => {
  if ($el?.length) {
    $el.find('#warn-dialog-submit').click();
  }
});

This script checks for the warning dialog by polling in small intervals. If the dialog appears within the specified attempts, it dismisses the dialog by clicking the submit button. Otherwise, it continues without causing the test to fail.

Alternative Approach

Another approach involves checking for the element’s existence using a more straightforward method within Cypress commands:

export function clickIfExist(element) {
  cy.get('body').then((body) => {
    if (body.find(element).length > 0) {
      cy.get(element).click();
    }
  });
}

This function checks if the element exists within the body and clicks it if found. This method is useful for simpler scenarios where the dialog is expected to appear quickly.

Conclusion

Handling elements that may not exist in Cypress tests can be challenging, but with the right approach, you can ensure your tests are robust and reliable. Using element polling or conditional checks within Cypress commands are effective strategies to manage such cases.

Enhancing Your Testing Workflow

For those looking to streamline their mobile app testing, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests quickly and efficiently. With features like computer vision and AI, Repeato ensures that your tests are fast and reliable. Additionally, Repeato’s intuitive test recorder and scripting interface cater to both beginners and advanced testers, making it a versatile tool for your testing needs.

Learn more about Repeato and how it can enhance your testing workflow by visiting our blog or checking out our detailed documentation.

Like this article? there’s more where that came from!