How to check for an element that may not exist using Cypress

48,787

Solution 1

There are various downsides to attempting to do conditional testing on DOM elements and also various workarounds in Cypress. All of this is explained in depth in the Cypress documentation in Conditional Testing.

Since there are multiple ways to do what you are trying to do, I suggest you read through the entire document and decide what is best for your application and your testing needs.

Solution 2

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

Solution 3

export const getEl = name => cy.get(`[data-cy="${name}"]`)

export const checkIfElementPresent = (visibleEl, text) => {
   cy.document().then((doc) => {
     if(doc.querySelectorAll(`[data-cy=${visibleEl}]`).length){
      getEl(visibleEl).should('have.text', text)

      return ;
   }
getEl(visibleEl).should('not.exist')})}
Share:
48,787
Charles Anderson
Author by

Charles Anderson

40 years or so as a professional software engineer, working in computer-aided design, petrochemical automation, semiconductor manufacturing, and data management. Most recent experience in JavaScript / Node.js, Python, HTML/CSS, and C++. Also Git, Mercurial, Jenkins, JIRA/Crucible, Less, Stylus, XML, UML. Now retired!

Updated on July 09, 2022

Comments

  • Charles Anderson
    Charles Anderson almost 2 years

    I am writing a Cypress test to log in to a website. There are username and password fields and a Submit button. Mostly logins are straightforward, but sometimes a warning dialog appears first that has to be dismissed.

    I tried this:

    cy.get('#login-username').type('username');
    cy.get('#login-password').type(`password{enter}`);
    
    // Check for a possible warning dialog and dismiss it
    if (cy.get('.warning')) {
      cy.get('#warn-dialog-submit').click();
    }
    

    Which works fine, except that the test fails if the warning doesn't appear:

    CypressError: Timed out retrying: Expected to find element: '.warning', but never found it.
    

    Then I tried this, which fails because the warning doesn't appear fast enough, so Cypress.$ doesn't find anything:

    cy.get('#login-username').type('username');
    cy.get('#login-password').type(`password{enter}`);
    
    // Check for a possible warning dialog and dismiss it
    if (Cypress.$('.warning').length > 0) {
      cy.get('#warn-dialog-submit').click();
    }
    

    What is the correct way to check for the existence of an element? I need something like cy.get() that doesn't complain if the element can't be found.

  • dewey
    dewey about 2 years
    Is there a way to set a timeout for finding the element?
  • M. Justin
    M. Justin almost 2 years
    @dewey The find method is a JQuery method, not a Cypress one, so it doesn't have any built-in timeouts. Here is a question regarding waiting until an element exists using JQuery or plain Javascript: stackoverflow.com/questions/5525071/…. I guess you could do something like this: if (body.find(element).length == 0) {cy.wait(timeout);} if (body.find(element).length > 0) {cy.get(element).click();}