typescript - 断言总是返回错误(期望 'a' 深度等于 'a' )

标签 typescript automated-tests assert e2e-testing testcafe

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

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

我真的试图找到一个解决方案,但要么我正在更改 letconst 的定义并尝试应用其他方法,但最终都以fail err,有不同的错误信息。 那么,在上述情况下,我该如何解决呢? 谢谢!

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

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

我的想法已经用完了:)

最佳答案

此问题与 nonbreaking space 有关.

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

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

关于typescript - 断言总是返回错误(期望 'a' 深度等于 'a' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877344/

相关文章:

typescript - vue类组件继承,方法来自基类 "is not a function"

angular - 无法使用 typescript 让插件与 wavesurfer.js 一起工作

python - 在 chrome 中运行 Selenium WebDriver python 绑定(bind)

java - 如何在Selenium中以不同的时间间隔截屏并将其保存在不同的位置?

go - 无法生成覆盖

reactjs - React useState Hook 错误 : Argument of type 'xxx' is not assignable to parameter of type 'SetStateAction<xx>'

ruby-on-rails - 带 rails 的 capybara : how to find ONLY invisible elements

c++ - 在 Release模式下是否忽略了 assert(false)?

c# - NUnit 嵌套集合比较

javascript - 根据路线隐藏和显示 Angular 4 组件