skip to Main Content

I have to verify a special character string on a webpage on different elements using Cypress.

Special character string is : T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi

Whenever I try to verify the string I get below assertion error in Cypress:

AssertionError Timed out retrying after 4000ms: Expected to find content: 'T!@$^()-+, &-=tt.Ø {y}[a] £ fi' within the element: <label> but never did.

Below is the screenshot:
enter image description here

Below is the code block:

    cy.visit('public/special-character.html')
    
    cy.get('label').contains('T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi');
    cy.get('p').contains('T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi');
   
  })

and also attached a sample HTML page:

<html><head>
<title>Special Character String</title>

<body>
<h3> Below is the special charcter string needs to be verified using Cypress</h3>
<label>T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi</label>
<p>T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi</p>
</body>
</head>
</html>

2

Answers


  1. What do you want to test?

    • If you want to get the element, get already fetches it. So no need for it. Otherwise, if you want to retrieve <label>, you can do your cy.contains at the top
    cy.contains('T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi').should('exist'); // return <label>
    
    • If you want to assert that the text contains the desired text, you can try the following:
    cy.get('label').should('have.text','T!@$^()-_+, &_-=tt.Ø {y}[a] £ fi');
    
    Login or Signup to reply.
  2. This looks like a bug.

    The part of the text =tt.Ø {y}[a] is causing it to fail. Looks like it’s evaluating to a function declaration.

    If you try that part of the text in isolation, it also fails

    cy.get('label').contains('=tt.Ø {y}[a]')  // fails
    

    but one char less either end passes

    cy.get('label')
      .contains('=tt.Ø {y}[a')
      .should('have.prop', 'tagName', 'LABEL')  // passes
    
    cy.get('label')
      .contains('tt.Ø {y}[a]')
      .should('have.prop', 'tagName', 'LABEL')  // passes
    

    Splitting the string and using .contains() on each section will also pass

    const splitChar = 'Ø'
    const [first, second] = specChars.split(splitChar) 
    cy.get('label')
      .contains(first)
      .contains(splitChar)
      .contains(second)
      .should('have.prop', 'tagName', 'LABEL')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search