--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
Generally it is because Thread.sleep() will introduce a fixed period of inactivity no matter what happens which will slow down your test, and if you are using CI generally result in much longer test run times if used all over the place.
Secondly most people who use it end up doing so because they don’t understand a problem so it’s quick and easy to just throw in an arbitrary wait rather than trying to understand the problem and fix it.
For example I perform an AJAX action and then have to wait for an element to appear, the correct thing to do would be to scan for the element until it appears and then carry on, but this is harder to code than to just throw in a 30 second wait so people use a Thread.sleep instead. Now the problem is that this means you don’t really know why the test has failed if the element is not picked up after your 30 second time out, is it because the element didn’t appear at all, or is it because the AJAX call took longer than normal to return? If you had some code that scanned for the element you could stick in a much longer timeout secure in the knowledge that you are not slowing your test down, and also secure in the knowledge that when it fails you can be sure that it is because the element didn’t return.
I’m sure people will argue that it is easier to use Thread.sleep and the difference in results as described above is not that great, but when you have 200 scripts getting run in CI and each one has about 5 Thread.sleeep calls you’ll see how much pain just throwing in a thread.sleep can cause.
The correct way would be to wait for button1 to appear before continuing your test. Did you actually read through what I wrote? J
Mark is right. An additional problem with fixed waits like Java’s Thread.sleep() is that they work at the time you write the test and on the machine where you’re writing it. Then the test gets run on a different machine, perhaps with less capacity, and the thing you’re actually trying to wait for takes longer to run. But that means your pre-calculated wait isn’t long enough for that machine.
In my case, I have a REALLY nice laptop that can run our entire product stack (a typical multi-tier web application) and the tests, while doing a variety of other things. It’s very fast, and very sweet J But our tests also run on every code check-in, using virtual machines in Amazon’s EC2 cloud, and ran on VMware virtual machines before we switched to EC2. Using waits the way Mark describes means that these tests behave reliably in this wide variety of environments, and also complete in the minimum possible time. That’s important to us, because we run about 1200 of them each time, and it takes a total of about 15 hours (of course, we run lots of them in parallel).
Ross
My $0.02 on it.
Mike
--
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
Your scenario is contrived and seems to be a way to justify the usage of Thread.sleep(). If you need to wait for up to 5 seconds set your wait to wait for 5 seconds, that way it will only wait for up to 5 seconds before failing.
In the screnario you describe Thread.Sleep() is still the wrong way to do it, Thread.sleep() is not the lesser of two evils, it is just a lazy way of makings things work rather then analysing the problem, understanding the problem and then fixing the problem.
From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Michael Hashimoto
Sent: 28 August 2011 08:33
To: seleniu...@googlegroups.com
Ross
-----Original Message-----
From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Mike
Sent: Friday, August 26, 2011 11:29 AM
To: Selenium Users
If the wait-for-condition would timeout because the XPath or ID has changed, then the test deserves to fail. In this case, either the application or the test is brittle, and the test will probably fail from time to time even when the application is verifiably correct. The only thing worse than not having a test is having a test that fails or passes when it should do otherwise.
The way around this is to find a 100% reliable wait-for condition, not to insert fixed-duration waits, and to fail as fast as possible, not to cruise on hoping for success.
Ross
Soso,IMHO, Thread.sleep() introduces a definite wait.For e.g., if you were using Thread.sleep() and causing the current thread to always sleep for 20 seconds, then irrespective of anything your test would always wait for 20 seconds.Now there are two sides of the story.There are always chances of your test taking more than 20 seconds to do some operation and you ending up trying to do the next action even when its not completed yet.Then there are other times, when your action finished well in advance, but your test still waits for 20 seconds.However, I have seen people circumvent this part, by having lower Thread.sleep() durations and then iterate within a loop for a max time before timing out.Maybe others can chime in and provide different perspectives on why they feel "Stay away from Thread.sleep()" :)
Thanks & Regards
Krishnan Mahadevan
"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
@Mr. KRISHNA MADHAVAN- Sir could make it simple, I mean an explination in simple words.