How do we know that the web page we are interested in has loaded and is ready for us to start running our scripts against? It sounds like a simple question; however, it is one of the things that always poses great problems to automation engineers. If you are ever asked why a script doesn’t work when the code seems sensible, your usual reply should probably be, It’s a waiting trap!
Let’s explore the root causes of these waiting traps. Suppose we have a page that waits until it is loaded to make an AJAX request to a server. This page is not ready to be used until the AJAX request is complete, but Selenium WebDriver thinks it is ready after the initial page load. How do we let Selenium know that the page has loaded and is ready for us to start our automated script?
I’ve timed it, and I know it takes 5 seconds for the page to load.
You will often see automation engineers say this and tend to add something like this to their scripts:
A strategy like this will never work reliably. Because it doesn’t take into account any external variables, and it will slow our tests down unapologetically. External variables that should be taken into account are:
A slow machine with very little memory will run our scripts much slower than a fast machine. This means that the web element that has been rendered instantly on a fast machine may not actually be there for a few hundred milliseconds on a slow machine. This is long enough to cause an error in our test, but not long enough for it to be instantly obvious to the human eye.
The server that is hosting the website that we are testing is dependent upon the design of the site and it can have varying effects especially when it depends upon a lot of server-side processing. It could slow down significantly when under load. If the site have a high number of concurrent users, the server may have problems servicing the requests, and the responses coming back to the client may take longer than expected.
In AJAX-heavy sites, network performance is very crucial. If an AJAX request take a long time to resolve, the site under test will take longer to rerender the frontend as a result of the AJAX calls.
If we start looking at all of these potential traps, it soon becomes clear that an arbitrary wait of 5 seconds is never going to be reliable. The reaction some automation engineers have when they are faced with this trap is that they extend their waits and falling into a chain of traps.
Simply telling automation engineers not to use Thread.sleep() is not going to fix the problem either. We need to explain why we should not use Thread.sleep().
In summary, we have identified the main culprits in slowing down our automated Selenium WebDriver tests. In my next blog, I’ll provide solutions to these waiting traps.
will wait next blog to see solutions )
Thanks Kamil for the comment. Sure will upload soon.
Yes. Facing and finding very challenging to handle waits and dynamic delays. will love to see a best practicing solutions.
Thanks Faisal. Yes I’ll upload best practices for dealing waits very soon.