Isha Pinjarkar
Isha Pinjarkar

Reputation: 83

Alternative way for "Thread.sleep(5000)" command in selenium webdriver

I need to use this "Thread.sleep(5000)" command after every action on my automation test cases. Is there any alternative way to skip writing this command after every action.

If I skip this command I got "time out" and "element not found" error.

Upvotes: 2

Views: 17668

Answers (2)

Jaroslav Cincera
Jaroslav Cincera

Reputation: 1036

Put Thread.sleep(5000) after each action is definitely not good idea. If you will have lot of tests then it will take to much time (like hours) to finish all of them.

For these cases there is waiting mechanism in WebDriver which basically synchronize actions in code and browser behavior so you don't get ElementNotFoundException because all actions will be synchronized.

You can see lot of examples on Selenium docs http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

Upvotes: 6

Anaxi
Anaxi

Reputation: 316

You can use an implicit wait. Add this line after you initialize your WebDriver: (example in C#)

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Quote from: https://stackoverflow.com/a/6993597/4741659

Upvotes: 5

Related Questions