Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active May 18, 2021 14:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elgalu/4f4735f04f1eb60a70b7 to your computer and use it in GitHub Desktop.
Save elgalu/4f4735f04f1eb60a70b7 to your computer and use it in GitHub Desktop.
Protractor Browser Capabilities Extensions
////////////////////////////////////////////////
// Protractor Browser Capabilities Extensions //
////////////////////////////////////////////////
"use strict";
module.exports = browser.getCapabilities().then(function(s) {
var browserName, browserVersion;
var shortName, shortVersion;
var ie, ff, ch, sa;
var platform;
platform = s.caps_.platform;
browserName = s.caps_.browserName;
browserVersion = s.caps_.version;
shortVersion = browserVersion.split('.')[0];
ie = /i.*explore/.test(browserName);
ff = /firefox/.test(browserName);
ch = /chrome/.test(browserName);
sa = /safari/.test(browserName);
if (ie) {
shortName = 'ie';
} else if (ff) {
shortName = 'ff';
} else if (ch) {
shortName = 'ch';
} else if (sa) {
shortName = 'sa';
} else {
throw new Exception('Unsupported browser: '+ browserName);
};
// Returns one of these: ['ch', 'ff', 'sa', 'ie']
browser.getShortBrowserName = function() {
return shortName;
};
// Returns one of these: ['ch33', 'ff27', 'sa7', 'ie11', 'ie10', 'ie9']
browser.getShortNameVersionAll = function() {
return shortName + shortVersion;
};
// Returns one of these: ['ch', 'ff', 'sa', 'ie11', 'ie10', 'ie9']
browser.getShortNameVersion = function() {
if (ie) {
return shortName + shortVersion;
} else {
return shortName;
};
};
// Return if current browser is IE, optionally specifying if it is a particular IE version
browser.isInternetExplorer = function(ver) {
if (ver == null) {
return ie;
} else {
return ie && ver.toString() === shortVersion;
}
};
// Function alias
browser.isIE = browser.isInternetExplorer;
browser.isSafari = function() {
return sa;
};
browser.isFirefox = function() {
return ff;
};
// Return if current browser is Chrome, optionally specifying if it is a particular Chrome version
browser.isChrome = function(ver) {
if (ver == null) {
return ch;
} else {
return ch && ver.toString() === shortVersion;
}
};
browser.inWindows = function() {
return /^WIN|XP/.test(platform);
};
browser.inOSX = function() {
return /^MAC/.test(platform);
};
// Save current webdriver session id for later use
browser.webdriverRemoteSessionId = s.caps_['webdriver.remote.sessionid'];
browser.inSauceLabs = function() {
return !!(browser.params.inSauceLabs);
};
browser.inBrowserStack = function() {
return !!(browser.params.inBrowserStack);
};
browser.inTheCloud = function() {
return !!(browser.params.inSauceLabs || browser.params.inBrowserStack);
};
});

Usage - Config file

onPrepare: require('./capabilities.js'),

Usage - Test files

if (browser.inOSX()) {
  // in Mac...
} else if (browser.inWindows()) {
  // in Windows...
} else {
  // likely in Linux...
}
@scottybrown
Copy link

This may fail JSHint (depending on your configuration) due to:

  • Use of '=='
  • Use of semi-colon after if/for statements

It'd be nice if these were fixed up :)

@bleurose
Copy link

Help. I tried using this EXACTLY as shown and I get the following error:

ERROR - failed loading configuration file ./protractor.conf.js
/usr/local/bin/node_modules/protractor/lib/configParser.js:183
    throw e;
          ^
ReferenceError: browser is not defined
    at Object.<anonymous> (/Users/jonrosen/repo/gyft-b2b-webapp/test/capabilities.js:3:18)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/jonrosen/repo/gyft-b2b-webapp/test/protractor.conf.js:21:14)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

This is a pretty "early" failure and it is based on the fact that your supplied function uses the "browser" global reference in the very first functional line. My config file is

exports.config = {
  jasmineNodeOpts: {
    isVerbose: true,
    defaultTimeoutInterval: 120000
  },
  allScriptsTimeout: 120000,
  getPageTimeout: 60000,
  seleniumAddress: "http://localhost:4444/wd/hub",
  specs: ["e2e/specs/login-spec.js",
          "e2e/specs/send-to-one-spec.js",
          "e2e/specs/upload-spec.js",
          "e2e/specs/reset-pswd-spec.js"],
  multiCapabilities: [
    //{browserName: "chrome"},
    {browserName: "firefox",
      firefox_binary: "/Applications/Firefox.app/Contents/MacOS/firefox",
      binary_: "/Applications/Firefox.app/Contents/MacOS/firefox"
    }
  ],

  onPrepare: require('./capabilities.js'),
};

Unless you have left something very important out of your description, this is what you said to do and it should work. But it doesn't.

Any thoughts? Thanks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment