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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 07:44:32

问题


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.


回答1:


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).




回答2:


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




回答3:


  • 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



来源:https://stackoverflow.com/questions/45584840/what-is-the-difference-between-browser-sleep-and-browser-wait-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!