Stop using time.sleep everywhere!



I think that almost every automation infrastructure that I have seen has the time.sleep() method at least once.

Sometimes it comes from laziness and sometimes from a lack of knowledge, but I believe that in at least 90% of the times, sleep methods should not appear in our code.

Why do we need the sleep method at all?

In the e2e automation world, when we try to automate the AUT (application under test), we sometimes depend on processes we do not control.
For example, as automation developers, when we click on a button to switch pages in the AUT, we don't have control on the pace that the new page is loaded, it depends on many variables like the network speed and communication with third party systems (like databases and servers).

When the automation developer starts an operation that can take time (like the page loading in the example) and continues his test flow right after, the test may fail due to missing information of the application. for example, if we try to click on buttons of a page that is not fully loaded, the test can fail with 'ElementNotFoundException'.

To prevent this from happening many developers just try to guess the time that it takes for the operation to complete, they insert a time.sleep(time_to_wait) method and continue with their lives.

So, what is the problem with the sleep method?

In my opinion, the time.sleep() method is a bad choice because many times the environment of our code varies, and an operation that took us 3 seconds yesterday, will not necessarily take us the same amount of time tomorrow. The network can suddenly respond slower, or the database connection can be flaky, and we don't have any control over these kind of things.
In addition, I think we almost always make the thread sleep for more time than needed, because we take some buffer.

What can we do instead?

The thing that we can know, or at least get a better understanding of, is the maximum time the operation can take.
After we know what is the maximum time required for the operation and what we are waiting for, we can start writing a method called wait_until()

The wait_until() method:

The whole purpose of the wait_until() method is to wait until a certain condition becomes true, the method will take as arguments a method which returns true or false if the condition is true,a timeout argument and an interval argument that says how much to wait before checking again. the method will look like this:

Let's go over the implementation and see what is going on there.
The method signature contains the func, the timeout, the interval, and the args and kwargs for func.
In the method body we define the maximum time the operation should take, and then loop on calling func until the maximum time passes or until the condition becomes true.
If we got to the timeout and the condition is not true yet, the method will throw a timeout exception.

If you already use an automation tool like Selenium or Appium, I would recommend you to use the built-in wait functions it has.

I added two tests to this method in order to make sure it functions properly.

But... it is not always practical to wait for something. In some cases there is no trivial way to check if the condition we are waiting for has become true, and in other cases it will be just too complicated to check it and not worth investing in. For example, when waiting for big systems to power on, and for all services to get started, it can be hard to know exactly for which service or program we are waiting.
When we handle one of these situations, we will just have to use the sleep method, but it's important to understand the scenario and make sure we know exactly what is the maximum time it can take for the operation to be performed.

To sum up

In general, using the wait method will save us time and effort by making sure we only wait for the event that we are interested in.

Until the next time...

תגובות

  1. The free spin bonus is one of the|is amongst the|is likely certainly one of the} most popular slot game options. Therefore, a slot machine software program development firm will definitely embrace it in your slot game. I-Slots are interactive slot games 코인카지노 that add more excitement for players. As a outcome, i-slots are more participating for the youthful generations used to enjoying in} video games. Progressive slots have the same guidelines because the traditional or some other slot game. However, the key thing} component of progressive slots is the progressive jackpot.

    השבמחק

הוסף רשומת תגובה

פוסטים פופולריים מהבלוג הזה

קודמתי לדרגת סיניור במיקרוסופט - מה למדתי בדרך

מהם קבצי DLL ואיך להשתמש בהם?

מדריך C# | שימוש ב LINQ