Protractor: Detect the type of browser the tests are running against

In my earlier posts here and here, I discussed how to configure protractor for single capabilities and multi-capabilities. In this post I will talk about how to detect the type of browser(s) that the tests are running against (ex. Chrome, Firefox etc.).

The reason for doing so, because often times we are facing problems where we want to define certain steps or features base a specific browser. For example, firefox is slower than chrome, so we might want to give certain test step an extra browser based wait, but we do not want to increase the wait time, hence the length of the entire test run, for all the browsers.

So in short, the way we are doing it is through adding a functon in onPrepare method in protractor configuration file as below:

One thing to take note here is that the function defined on line 20 is merelly a promise, which we need to resolve, so that the brower type can be evaluated. HenceĀ  we need to invoke the method getBrowserName() by calling browser.getBrowserName() in order to retrieve and assign the browser name to variable browser.browserName.

So later on if we ever want to check the browser type (i.e. browserName) once the tests are running, we could easily retrive the value from anywhere in the tests definitions, for example:

From here we can see to test the type of browser we just need to retrieve this value, compare it, and performance appropriate actions accordingly.

 

Reference:

[1] https://stackoverflow.com/questions/38503953/protractor-get-browser-title-as-string

[2] https://stackoverflow.com/questions/38344805/detecting-browser-with-protractor

[3] https://stackoverflow.com/questions/28180149/get-current-name-in-protractor-multicapabilities-config

[4] https://stackoverflow.com/questions/22917219/protractor-accessing-capabilities

[5] https://stackoverflow.com/questions/31203398/protractor-set-global-variables

[6] https://github.com/angular/protractor/issues/3036

[7] https://github.com/IgorSasovets/get-config-capabilities

 

  1 comment for “Protractor: Detect the type of browser the tests are running against

  1. April Gibson
    March 25, 2020 at 1:53 PM

    Thank you for your post. This worked perfectly for me.

Leave a Reply

Your email address will not be published. Required fields are marked *