As I discussed earlier, Thread.Sleep() is a big hammer for performance, because based on the MSDN documentation, it blocks the current thread for the specified number of milliseconds1. This technique becomes a problem when your responses are faster than the sleep time, leading to performance issues.
Ajax may become a headache when you are automating web applications because you can’t control in some way the exactly elapsed time of an operation to complete. And indirectly you can have several non-deterministic test due to a wrong handling of the Ajax response.
But, how to deal with asynchronous rendering text?
This is a good use case. Let’s suppose that you are providing wrong credentials in your login automation and the expected message is Invalid username or password . The name of the html label is lblErrorMessage and let’s say that our application relies on Ajax. Therefore at the time of submitting the form, a validation is performed asynchronously and the error message is displayed.
How can you know how long time is going to take the SOAP or REST service to respond?, How can you know how long time is the database going to take to execute the stored procedure? (even worse if there are jobs being executed). The short answer is you can’t .
Considering this scenario you probably would like to use Thread.Sleep() method and wait for 5 seconds when the error message should already be visible and rendered. Now you are very proud because the test works as expected.
The problem here is that you have several test that do the same thing, they use a Sleep() of 5 seconds over and over and over again. This becomes a problem because while the process is waiting for a certain amount of seconds, the underlying transaction finished few seconds ago. So we have a performance issue to deal with.
How to solve this problem?
Doing a short review, our facing issue is to determine how long the browser automation needs to wait until the error message is displayed without fall into a performance issue. Fortunately,Selenium provides a wait mechanism that can be used, based on that documentation, the wait can be either explicit or implicit.
Using the power of lambda expressions and Implicit waits we can determine when the error has occurred by waiting until our label contains text (WebDriver wait until uses boolean expressions to finish the waiting time).
In the following code snippet, we wait until the error message contains text. Since the name of the method is SignUpAsExpectingError, the method is designed to capture the error message as a result of values that are not valid.
This error message label should be empty by default and it should have text just after something happened. Therefore the Wait mechanism is tied to another HTML fields that are a kind of flags.
This approach helps considerably to avoid performance issues. There is a more complicated case when the HTML element always contain text (it is not empty by default) so you need to identify in what field to rely.