2

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 :)

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Eugen
  • 785
  • 6
  • 22

2 Answers2

5

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€');
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Artem
  • 921
  • 4
  • 11
2

The issue with your specific test case is that the &nbsp; is not interpreted by Testcafe to be a regular space.

What will work is if you copy the 12,45 € from the error message you get, paste it in the code as the expected value and that's it.

fjc
  • 5,590
  • 17
  • 36