Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between browser.sleep() and browser.wait() methods?

Facing timing issue with protractor. sometimes my protractor test cases fails due to network or performance issues. I have solved existing issues with browser.sleep(). Later I came to know about browser.wait().

What is the difference between them and which one is better for solving network or performance issues.

like image 468
Anik Saha Avatar asked Aug 09 '17 07:08

Anik Saha


3 Answers

When it comes to dealing with timing issue, it is tempting and easy to put a "quick" browser.sleep() and move on.

The problem is, it would some day fail. There is no golden/generic rule on what sleep timeout to set and, hence, at some point due to network or performance or other issues, it might take more time for a page to load or element to become visible etc. Plus, most of the time, you would end up waiting more than you actually should.

browser.wait() on the other hand works differently. You provide an Expected Condition function for Protractor/WebDriverJS to execute and wait for the result of the function to evaluate to true. Protractor would continuously execute the function and stop once the result of the function evaluates to true or a configurable timeout has been reached.

There are multiple built-in Expected Conditions, but you can also create and use a custom one (sample here).

like image 156
Upalr Avatar answered Sep 26 '22 22:09

Upalr


  • sleep: Schedules a command to make the driver sleep for the given amount of time
  • wait: Schedules a command to wait for a condition to hold or promise to be resolved.

Reference for detail: http://www.protractortest.org/#/api?view=webdriver.WebDriver.prototype.sleep

like image 43
Dao Minh Dam Avatar answered Sep 25 '22 22:09

Dao Minh Dam


browser.sleep()

Schedules a command to make the driver sleep for the given amount of time.

browser.wait()

Schedules a command to wait for a condition to hold or promise to be resolved.

This function blocks WebDriver's control flow, not the javascript runtime. It will only delay future webdriver commands from being executed (e.g. it will cause Protractor to wait before sending future commands to the selenium server), and only when the webdriver control flow is enabled.

Documentation link http://www.protractortest.org/#/api

like image 42
Optimworks Avatar answered Sep 25 '22 22:09

Optimworks