What is the difference between browser.sleep() and

2019-05-01 06:02发布

问题:

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