testcafe 中的自定义函数

Custom function in testcafe

所以,我正在尝试创建一个自定义函数,它允许我检查一个字段是否包含数字或文本,但为了进一步测试,我将需要检查更复杂的东西,比如一些东西的总和table 等于某物,等等。 我找不到自定义函数的示例,例如:

function isNumber(n) {
  let a = parseInt(n);
  if (a > 0 || a < 0) {
    return true
  } 
  return false
}
test('Test example', async t => {
  await t
      .expect(isNumber(Selector('#thisNum').innerText)).ok('This is a number' );
    
});

只会显示断言消息when the assertion fails(参考消息参数)。例如,

await t
  .expect(failingValue).ok('failingValue is not a number');

在测试失败时会显示如下内容:

1) AssertionError: failingValue is not a number: expected false to be truthy

因此,我从没想过会看到 "This is a number" 消息。

至于函数,我遇到过几次 promise 尚未解决的情况,所以请尝试等待号码选择器:

await t
  .expect(isNumber(await Selector('#thisNum').innerText)).ok('This is a number');

希望这对您有所帮助。