Skip to content

Assertions

rquellh edited this page May 9, 2018 · 22 revisions

Assertions are a way to assess if the web page is in an expected state. We are only going to cover a few assertion commands in this exercise, but there are plenty that TestCafe can perform.

Smart Assertions

TestCafe handles assertions differently than many other assertion libraries. Some libraries immediately fail the assertion if the assertion is initially false. On the other hand, TestCafe retries the assertion until the assertion completes successfully or the step times out. This cuts down on the amount of failures that occur due to timing errors.

smart assertions

Truthy and Falsy

Another important concept to understand is truthy and falsy. Truthy values are values that have a true outcome when there is a Boolean evaluation, and the inverse is true for falsy values.

There are some values that are inherently falsy:

  • false
  • 0 (zero)
  • '' or "" (empty string)
  • null
  • undefined
  • NaN (e.g. the result of 1/0)

All other values are inherently truthy including:

  • '0' (a string containing a single zero)
  • 'false' (a string containing the text “false”)
  • [] (an empty array)
  • {} (an empty object)
  • function(){} (an “empty” function)

It's important to have an understanding of what's truthy and falsy. A good resource is for this is https://www.sitepoint.com/javascript-truthy-falsy/.

Creating Assertions

sample page

In the