5

If I want to use implicitlyWait, where I should put browser.manage().timeouts().implicitlyWait(5000); in the test?

giri-sh
  • 6,934
  • 2
  • 25
  • 50
winlinuz
  • 171
  • 1
  • 3
  • 13

1 Answers1

13

Add it in the onPrepare() function of your protractor's conf.js file. The reason to add implicitlyWait() there is because implicit wait is the default time that protractor waits before passing or throwing an error for an action. Letting protractor know what the implicit wait time is, even before the tests start is the best way to make use of it and onPrepare() function runs before all test suites and only once.

Example scenario:

Suppose you have the below line of code -

element(LOCATOR).getText();

in your test spec and protractor executes it after initiating the automation on page. Now, if the element with the locator specified is not found on the page, then protractor doesn't throw an error immediately, but it waits for the implicit wait time to complete. Meanwhile until implicit timeouts, it checks if the element can be located on the DOM. At the end of the implicit wait time if the element is not found, then the protractor throws the respective error. So for all the operations that you perform it is necessary to let protractor know the implicit wait time well before hand.

Usage:

onPrepare: function(){
    browser.manage().timeouts().implicitlyWait(5000);
},
giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • Thank you very much! You've helped me a lot to understand. – winlinuz Oct 23 '15 at 10:10
  • Does, browser.manage().timeouts().implicitlyWait(5000); also wait for visibilty of element or presence of element in DOM? [Girish](https://stackoverflow.com/users/4180674/girish-sortur) – Vikas Gupta Oct 06 '17 at 11:06
  • If element is present in DOM, but not visible, then in this case, Does protractor implicit wait for it before throwing an error? ("element is not visible?") – Vikas Gupta Oct 06 '17 at 11:08
  • 1
    @Vikash.777 it should wait for every action you perform on a webpage from the last operation before throwing an error. Also it doesn't wait if the page has already loaded completely. Use Explicit wait if you want to check for the visibility of the element. – giri-sh Oct 09 '17 at 02:50