Skip to content

Commit

Permalink
Using semver for flexible browser version detection
Browse files Browse the repository at this point in the history
This change is backward incompatible; the old config properties
`majorVersion`, `minorVersion` and `revision` were replaced with
semver-compliant `browserVersion`.

Close attester#40.
  • Loading branch information
jakub-g committed Jul 29, 2013
1 parent ccd5c26 commit eda684c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,36 @@ browsers:
# However, if the browsers section is present, each test will be run once in each browser listed here.
# (and browsers not listed here will have nothing to do if they are connected)
- browserName: 'PhantomJS'
- browserName: 'Chrome'
- browserName: 'Opera'
- browserName: 'Safari'
# It's possible to distinguish browsers by operating systems, read more below
- browserName: 'Firefox'
- browserName: 'Chrome'
os: 'Windows 7'
- browserName: 'Firefox'
- browserName: 'Chrome'
os: 'Desktop Linux'
- browserName: 'Firefox'
- browserName: 'Chrome'
os: 'Android'
- browserName: 'Opera'
- browserName: 'Safari'
# It is also possible to distinguish several versions of the same browser:
- browserName: 'IE'
majorVersion: 7
browserVersion: 7
- browserName: 'IE'
majorVersion: 8
browserVersion: 8
- browserName: 'IE'
majorVersion: 9
browserVersion: 9
- browserName: 'IE'
majorVersion: 10
# Note that 'minorVersion' and 'revision' are also available
# The 'name' property allows to change the display name of the browser in the reports.
browserVersion: 10
# 'browserVersion' can be also a semver-compliant string; see https://github.com/isaacs/node-semver
- browserName: 'Firefox'
browserVersion: '3.6'
- browserName: 'Firefox'
browserVersion: '>=20'
# Additionally, the 'name' property allows to change the display name of the browser in the reports.
- browserName: 'Chrome'
browserVersion: 27
name: 'Chrome Stable 27'
- browserName: 'Chrome'
browserVersion: 29
name: 'Chrome Canary 29'
```
#### Regarding browser detection by operating system:
Expand Down
25 changes: 19 additions & 6 deletions lib/browser-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@
* limitations under the License.
*/

var semver = require('semver');
var uaParser = require('ua-parser');

// semver-compliant version must be x.y.z
var toSemVer = function (version) {
var nbDots = (version.match(/\./g) || "").length;
if (nbDots === 0) {
return version + ".0.0";
} else if (nbDots === 1) {
return version + ".0";
} else {
return version;
}
};

exports.detectBrowser = function (data) {
var parsedAgent = uaParser.parse(data.userAgent);
var ua = parsedAgent.ua;
var osFamily = parsedAgent.os.family;
var res = {
displayName: parsedAgent.toString(),
semverString: toSemVer(parsedAgent.toVersionString()),
family: ua.family,
major: ua.major,
minor: ua.minor,
Expand Down Expand Up @@ -49,6 +63,7 @@ exports.detectBrowser = function (data) {
res.minor = 0;
res.patch = null;
res.displayName = "IE " + documentMode + ".0";
res.semverString = documentMode + ".0.0";
}
}
return res;
Expand All @@ -59,11 +74,9 @@ exports.browserMatch = function (config, browserInfo) {
if (config.browserName != null) {
match = match && (config.browserName == browserInfo.family);
}
if (match && config.majorVersion != null) {
match = config.majorVersion == browserInfo.major;
}
if (match && config.minorVersion != null) {
match = config.minorVersion == browserInfo.minor;
if (match && config.browserVersion != null) {
// user may pass version as a number, hence using String()
match = semver.satisfies(browserInfo.semverString, String(config.browserVersion));
}
if (match && config.os != null) {
// a little bit verbose to have readable code
Expand All @@ -77,4 +90,4 @@ exports.browserMatch = function (config, browserInfo) {
}
}
return match;
};
};
12 changes: 3 additions & 9 deletions lib/test-campaign/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ var buildNameFromConfig = function (config) {
var name = "";
if (config.browserName) {
name = config.browserName;
if (config.majorVersion != null) {
name += " " + config.majorVersion;
if (config.minorVersion != null) {
name += "." + config.minorVersion;
if (config.revision != null) {
name += "." + config.revision;
}
}
if (config.browserVersion != null) {
name += " " + config.browserVersion;
}
}
if (config.os) {
Expand Down Expand Up @@ -72,4 +66,4 @@ Browser.prototype.onTaskFinished = function () {
this.pendingTasks--;
};

module.exports = Browser;
module.exports = Browser;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"node-coverage": "1.0.2",
"optimist": "0.6.0",
"portfinder": "0.2.1",
"semver": "2.0.11",
"send": "0.1.0",
"socket.io": "0.9.16",
"ua-parser": "0.3.3",
Expand Down

0 comments on commit eda684c

Please sign in to comment.