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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
const browserConfig = require('./browserConfig'); exports.config = { // <other code and configuration goes here> // This is needed to run tests on firefox // This configuration does not interfere with operation of other browser directConnect: true, // The following line enable multi capabilities meaning running tests on more than // one browser: multiCapabilities: appiumCapabilities['multiBrowsers']; // This is needed to run sequentially in multiCapability, i.e. one browser at a time maxSessions: 1; onPrepare: function() { // obtain browser name browser.getBrowserName = function() { return browser.getCapabilities().then(function(caps) { browser.browserName = caps.get('browserName'); } )} // resolve the promised so the browser name is obtained. browser.getBrowserName(); } }; |
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:
1 2 3 |
if (browser.browserName === 'firefox') { FirefoxDoubleClick.onTextField('aField'); } |
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
Thank you for your post. This worked perfectly for me.