最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between browser.sleep() and browser.wait() methods? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 3, 2017 at 8:22 Anik Saha asked Aug 9, 2017 at 7:58 Anik SahaAnik Saha 4,4942 gold badges29 silver badges44 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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

  • 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

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

发布评论

评论列表(0)

  1. 暂无评论