12

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

Brendan
  • 4,327
  • 1
  • 23
  • 33
Venkata
  • 656
  • 6
  • 17

3 Answers3

15

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

Zain
  • 339
  • 2
  • 9
5

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')
Jennifer Shehane
  • 6,645
  • 1
  • 30
  • 26
0

I assume you are trying to to test that .c-ledger__row-amount is "DKK 15.00". Try this.

cy.get(".c-ledger__row-amount").invoke('text').then((text) => {
         expect(text.trim()).equal('DKK 15.00');
});

Sometimes you have to remove the trailing spaces

Maccurt
  • 12,655
  • 7
  • 32
  • 43
  • 1
    helpful note but this doesn't solve the OP's issue as there are no trailing spaces in the DOM screenshot attached – Sgnl Apr 03 '19 at 01:19