Search code examples
javascripttestingautomated-testse2e-testingtestcafe

Custom function in testcafe


So, i am trying to create a custom function that will allow me to check if a field contains a number or a text, but for further test i will need to check more complex stuff, like if the sum of some table equals something, etc. I can't find examples of custom functions for example:

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


Solution

  • The assertion message will only be displayed when the assertion fails (refer to the message parameter). For example,

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

    Would display something like the following on a failed test:

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

    Therefore, I'd never expect to see the "This is a number" message displayed.

    As for the function, I've experienced a couple of instances where the promise wasn't resolved yet, so try awaiting the number selector:

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

    Hope this helps.