Question: Do you use WebDriverWait instead of Asserts?

I watched some of your lectures and webinars and it looks like you are using WebDriverWait partly as a substitute for assertions. Is that normal?

WebDriverWait will throw an exception if it can’t find the item. Which has a similar effect to an Assert i.e the @Test will fail. I do not view this as an assert, I view this as setting up the necessary preconditions for the test, rather than a specific ‘check’ that the @Test enforces.

I explicitly use Asserts when the Assert describes something important for the test. So when people read the test they know what I am ‘testing for’.

Sometimes I add an Assert after a WebDriverWait, even though I know the Assert will never be thrown if the WebDriverWait fails, because in the context of the code, the Assert inderal to buy documents something important for the test.

I do not create tests without Asserts in production, even if a wait could act as an Assert.

Upon review I’d consider the test unclear, and I’d have to add a comment saying “no assert because the wait will fail”. Also it would mean a reliance on abstraction layers or Waits, but the system may change over time and the abstractions and necessary Waits may also change. But since I encode the test rationale in the Asserts, if all the Asserts become irrelevant and we remove them, then we also remove the need for the @Test itself.

Also I don’t add asserts to my abstraction layers, but I do add WebDriverWaits.

Semantically they differ, even if the end result of using them might seem the same.

This entry was posted in FAQ, Selenium Simplified Blog. Bookmark the permalink.

One Response to Question: Do you use WebDriverWait instead of Asserts?

  1. Kent says:

    Very glad you touched on this. I’ve often done the same thing (wait, then assert for the very same thing) and wondered if that was bad style and if it might be better to do without the assertions at all. Your post clearly describes why there should always be an assert (for clarity).

    Cheers!

    -Kent

Leave a Reply

Your email address will not be published. Required fields are marked *