Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting text of element with &nbsp

I need to check the text of a Div tag and ensure if it is showing the correct text of not

here is the HTML Code:

HTML Tag

Here is the step definition i wrote to assert the content

    Given(/^offer summary section should display "([^"]*)" amount against "([^"]*)"$/, (charge, labelText) => {
    const parentElement = cy.get('.c-offer-summary > .c-offer-summary__ledger').children('.c-ledger__section').find('.c-ledger__row-name').contains(labelText).parent();
    parentElement.find('.c-ledger__row-amount').invoke('text').should('eq',charge);
});

But cypress is throwing an error:

Error

I am not sure where i am going wrong !!! :(

any help is appreciated

like image 571
Venkata Avatar asked Nov 19 '18 16:11

Venkata


2 Answers

Based on Maccurt's assertion (which gives more context to the assertion information printed in Cypress logs) and Hiram's replace function, I composed this assertion.

cy.get(".c-ledger__row-amount").invoke('text').then((text) => {
    expect(text.replace(/\u00a0/g, ' ')).equal('DKK 15.00');
});

It works well on my case. Here's a screenshot for the assertion result: enter image description here

like image 88
Zain Avatar answered Sep 22 '22 20:09

Zain


You could use Cypress's .contains() command which strips whitespaces:

cy.get('.c-ledger__row-amount').contains('DKK 15.00')

Or you can remove the space character then assert:

cy.get('.c-ledger__row-amount')
  .invoke('text')
  .invoke('replace', /\u00a0/g, ' ')
  .should('eq', 'DKK 15.00')
like image 24
Jennifer Shehane Avatar answered Sep 23 '22 20:09

Jennifer Shehane