断言总是 return 错误(预期 'a' 深度等于 'a')

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

我在这里做一个简单的测试,但是我看到很多人无意中遇到了这个问题,但不幸的是,我找不到解决方案,所以,因此,我要求你的意见。 现在,我有这个字符串对象,在 link:

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

我创建了这个小测试来检查字符串值:

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 €');
});

我在终端中得到的错误是这个:

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

我真的想找到一个解决方案,但要么我在 let 中更改 const 的定义并尝试应用其他方法,但都以失败告终,不同的错误信息。 那么,在上述情况下,我该如何解决呢? 谢谢!

编辑:感谢提示!我编辑了 post,因为我意识到我没有提到我已经尝试过您建议的内容...

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 €');

错误:

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

再试一次:

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, ''));

错误

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

我 运行 没主意了 :)

您的特定测试用例的问题是 &nbsp; 未被 Testcafe 解释为常规 space。

如果您从收到的错误消息中复制 12,45 €,然后将其作为预期值粘贴到代码中,就可以了。

此问题与 nonbreaking space 有关。

以下 eql assertion 应该在您的场景中正常工作:

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