Protractor : use browser or browser.driver methods

2020-03-24 06:01发布

问题:

When using protractor, the global variable browser appears to have all the functionality of browser.driver.

I am specifically asking this because I am not sure whether to use browser.wait or browser.driver.wait as they both appear to be the same method, and I also saw that a lot of the browser.driver methods are available in browser (if not all).

So what is the recommended way to call those methods browser.method or browser.driver.method?

回答1:

In theory the distinction is simple: If this is an Angular application under test - use browser, otherwise - browser.driver.


A little bit more to the story:

Protractor wraps around WebDriverJS - javascript selenium bindings - as a part of that it wraps the selenium driver object itself leaving you the access to the pure WebDriverJS driver via browser.driver.


There are though other takeaways, please take a look at this related threads:

  • Protractor browser.driver.getCurrentUrl vs browser.getCurrentUrl
  • browser.driver vs browser. Which one to use?


回答2:

Some browser methods are the same...

The browser object is made up by composition of WebDriver methods and Protractor specific methods. So methods like sleep, wait, and getCurrentUrl are copied over from WebDriver (see the browser.ts). So should you use browser or browser.driver? Well, if it is listed in the link above, they are the exact same thing.

Some browser methods are not...

However, not every method is just copied over. For methods like get, the browser is implemented differently in Protractor vs selenium-webdriver. For Angular pages, you should use browser.get. This will wait for Angular to be stable before moving on to other commands before moving onto other commands.

When in doubt, check out the documentation

So when you navigate to protractortest.org/#/api, you will see a list of browser methods that are Protractor specific and "inherited from webdriver.WebDriver". The methods that follow "inherited from webdriver.WebDriver" are the same method if you decide to use browser or browser.driver.