首页 > 解决方案 > 断言总是返回 err(期望 'a' 完全等于 'a')

问题描述

我在这里做一个简单的测试,但是我看到很多人偶然发现了这个问题,但不幸的是,我无法找到解决方案,因此,我正在征求您的意见。现在,我在链接中有这个字符串对象:

...
<div class="price">12,45&nbsp;€&lt;/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 €'

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

编辑:感谢您的提示!我编辑了这篇文章,因为我意识到我没有提到我已经尝试过你的建议......

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

我在这里没有想法了:)

标签: typescriptautomated-testsasserte2e-testingtestcafe

解决方案


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

推荐阅读