question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Text values encoded with   can't be asserted

See original GitHub issue

Reproduced on version 6.0.1

Have a page with markup like: <div class="myClass">hello&nbsp;world</div>

run cypress with test like:

cy.get('.myClass').should('have.text', 'hello world')
cy.get('.myClass').should('have.text', 'hello&nbsp;world')
cy.get('.myClass').should('contain.text', 'hello world')
cy.get('.myClass').should('contain.text', 'hello&nbsp;world')
cy.get('.myClass').contains('hello world').should('exist')
cy.get('.myClass').contains('hello&nbsp;world').should('exist')

All mentioned tests fails, and there does not seem to be any way to verify the content without doing extra text manipulation first.

On top of that, both the AssertionError in the left side-bar and in the console log shows the same counterproductive message of:

expected ‘<div.myClass>’ to have text ‘hello World’, but the text was ‘hello World’

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jennifer-shehanecommented, Dec 7, 2020

I wrote a few ways that you could assert on this below.

The assertion in your tests has all lowercase ‘hello world’ and the Div has ‘hello World’ with a capital W. If you want it to not match the exact casing, then you need to pass: { matchCase: false } to .contains(), but any other assertion would fail without the exact casing.

it('text', () => {
  cy.visit('index.html')
  cy.get('.myClass').contains('hello world')

  cy.get('.myClass').invoke('text').then((text) => {
    // remove the space char
    expect(text.replace(/\u00a0/g, ' ')).equal('hello world')
  })

  cy.get('.myClass').invoke('text')
  // remove the space char
  .invoke('replace', /\u00a0/g, ' ')
  .should('eq', 'hello world')
})
1reaction
bahmutovcommented, Dec 17, 2020

@kiwdahc I have added example for your case here https://github.com/cypress-io/cypress-fiddle/commit/9dec37dbbefa0572dcdb3c3b63bb1cfaab43eecd

non-breaking-space

Short story: .filter(':contains... uses jQuery contains selector, thus you need to use the unicode character for non-breaking space entity which is \u00a0.

.filter(':contains("GBP\u00a00.50")')

I will add notes to your docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asserting text of element with &nbsp - mocha.js - Stack Overflow
I assume you are trying to to test that .c-ledger__row-amount is "DKK 15.00". Try this. cy.get(".c-ledger__row- ...
Read more >
Non-breaking space | Cypress examples (v6.5.0)
When using cy.contains command Cypress automatically converts the   entity into space character. Thus cy.contains finds element that include   .
Read more >
getting error: Neither the SAML assertion nor response is signed in ...
I am able to authenticate to the IDP but the response has some issues when AssertionConsumerService is called, specifically when samlServiceProvider.
Read more >
MUnit assert HTTP status code and response headers in Mule 4
Hey Guys. I'm trying to figure out how to assert HTTP status code on my MUnit test case. The assertion that I would...
Read more >
dxml.util - jmdavisprog.com
"Normalizes" the given text and transforms character references into the ... can't be directly represented in the text fields or attribute values in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found