Search code examples
typescriptautomated-testsasserte2e-testingtestcafe

Assertion always return err (expected 'a' to deeply equal 'a')


I'm doing a simple test here, but I've seen a lot of ppl stumbled into this problem, but unfortunately, I wasn't able to find a solution, so, therefore, I'm asking for your opinion. Now, I have this string object, inside a link:

...
<div class="price">12,45&nbsp;€</div>
...

I created this small test, to check the string value:

import { t, Selector } from 'testcafe';
fixture `OfferPage`.page `https://www.verivox.de/applications/broadband/#/offer?i=eyJmaWx0ZXIiOltudWxsLDE2MDAwLDEsbnVsbCwiMDIyMSIsMSwxLDEsbnVsbCwxLDEsbnVsbCwtMSxudWxsLG51bGwsInByaWNlLWFzYyIsMixudWxsLG51bGwsbnVsbCxudWxsLG51bGwsbnVsbCxudWxsLG51bGwsNl0sImRpYWxvZyI6W251bGxdLCJvZmZlckNyaXRlcmlhIjpbIjYxMzQ0NyIsIjE4MjkyIixudWxsLCIyNCIsMywyLCI1MDAwMCIsIjEwMDAwIixudWxsLG51bGwsMSxudWxsLG51bGwsbnVsbCwiMiIsMSxudWxsLCIwMjIxIixudWxsLG51bGwsbnVsbCxudWxsLG51bGwsMSw2LG51bGwsbnVsbCxudWxsXX0%3D`;
test('1', async () => {
    const string = Selector('div.price');
    await t.expect(string.innerText).eql('12,45 €');
});

The error I get in terminal is this one:

AssertionError: expected '12,45 €' to deeply equal '12,45 €'

I really tried to find out a solution, but either I'm changing the definition from const in let and trying to apply other methods, all end up in a fail err, with different error messages. So, how could I sort things out, in the above case? Thanks!

EDIT: Thanks for the hints! I edited the post because I realized I haven't mentioned that I already tried what you suggested ...

let price = Selector('div').withAttribute('class', 'price');
const result = price.parent('div.centered-content effective-price-wrapper');
console.log(result);
await t.expect(result.innerText).eql('12,45 €');

err:

Cannot obtain information about the node because the specified selector does not match any node in the DOM tree.

another try:

const string = await Selector('div.price')();
let pret = await Selector(string).innerText;
const rgx = /&nbsp;/gi;
await t.expect(pret.replace(rgx, '')).eql('12,45 €'.replace(rgx, ''));

err

 AssertionError: expected '12,45 €' to deeply equal '12,45 €'

I'm running out of ideas here :)


Solution

  • This issue is related to the nonbreaking space.

    The following eql assertion should work properly in your scenario:

    await t.expect(string.innerText).eql('12,45\xa0€');