Skip to content

Commit

Permalink
Upgrade tests to work with the new chromedriver
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed May 23, 2017
1 parent 026e9be commit e7d7121
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const gulp = require('gulp');
const gutil = require('gulp-util');
const webdriver = require('gulp-webdriver');
const gzipSize = require('gzip-size');
const path = require('path');
const {rollup} = require('rollup');
const nodeResolve = require('rollup-plugin-node-resolve');
const babel = require('rollup-plugin-babel');
Expand Down Expand Up @@ -90,7 +91,7 @@ gulp.task('javascript:unit', ((compiler) => {
return webpack({
entry: glob.sync('./test/unit/**/*-test.js'),
output: {
path: 'test/unit',
path: path.resolve(__dirname, 'test/unit'),
filename: 'index.js',
},
devtool: '#source-map',
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
"dependencies": {
"chalk": "^1.1.3",
"dom-utils": "^0.9.0",
"fs-extra": "^2.0.0",
"fs-extra": "^3.0.1",
"glob": "^7.1.1",
"google-closure-compiler-js": "^20161201.0.1",
"google-closure-compiler-js": "^20170423.0.0",
"gzip-size": "^3.0.0",
"rollup": "^0.41.4",
"rollup-plugin-memory": "^2.0.0",
"rollup-plugin-node-resolve": "^2.0.0",
"rollup-plugin-node-resolve": "^3.0.0",
"source-map": "^0.5.6"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-loader": "^7.0.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-register": "^6.22.0",
Expand All @@ -67,8 +67,8 @@
"source-map-support": "^0.4.10",
"uuid": "^3.0.1",
"wdio-mocha-framework": "^0.5.8",
"wdio-sauce-service": "^0.3.1",
"wdio-sauce-service": "^0.4.0",
"webdriverio": "^4.6.1",
"webpack": "2.1.0-beta.19"
"webpack": "2.6.0"
}
}
45 changes: 45 additions & 0 deletions test/e2e/fixtures/page-visibility-tracker-pageload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;

ga('create', 'UA-12345-1', {
trackingId: 'UA-12345-1',
cookieDomain: 'auto',
siteSpeedSampleRate: 0,
});

var testId = location.search.match(/testId=([\w\-]+)/)[1];
ga(function (tracker) {
var oldSendHitTask = tracker.get('sendHitTask');
tracker.set('sendHitTask', function (model) {
var hitIndex = +(localStorage.getItem('hitcounter') || -1) + 1;
var hitTime = +new Date() - (model.get('queueTime') || 0);
var hitPayload = model.get('hitPayload') +
'&time=' + hitTime +'&index=' + hitIndex;

oldSendHitTask(model);

if ('sendBeacon' in navigator) {
navigator.sendBeacon('/collect/' + testId, hitPayload);
} else {
var beacon = new Image();
beacon.src = '/collect/' + testId + '?' + hitPayload;
}
localStorage.setItem('hitcounter', hitIndex);
});
});

ga('require', 'pageVisibilityTracker', {
sendInitialPageview: true,
visibleThreshold: 0,
pageLoadsMetricIndex: 1,
});
</script>
<script async src="/test/analytics.js"></script>
<script async src="/autotrack.js"></script>
</head>
<body>
</body>
</html>
53 changes: 15 additions & 38 deletions test/e2e/page-visibility-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,16 @@ describe('pageVisibilityTracker', function() {
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);
browser.pause(500);

const backgroundTab = openNewTabInBackground(
'/test/e2e/fixtures/autotrack.html?tab=2');

browser.switchTab(backgroundTab);
browser.execute(ga.run, 'create', DEFAULT_TRACKER_FIELDS);
browser.execute(ga.logHitData, testId);
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);
openNewTabInBackground('/test/e2e/fixtures' +
'/page-visibility-tracker-pageload.html?testId=' + testId);

browser.waitUntil(log.hitCountEquals(2));

const hits = log.getHits();
assert(hits[0].dl.endsWith('tab=1'));
assert.strictEqual(hits[0].t, 'pageview');
assert.strictEqual(hits[0].cm1, '1');
assert(hits[1].dl.endsWith('tab=2'));
assert(hits[1].dl.includes('page-visibility-tracker-pageload.html'));
assert.strictEqual(hits[1].ec, 'Page Visibility');
assert.strictEqual(hits[1].ea, 'page load');
assert.strictEqual(hits[1].ni, '1');
Expand All @@ -350,31 +345,20 @@ describe('pageVisibilityTracker', function() {
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);
browser.pause(500);

const backgroundTab = openNewTabInBackground(
'/test/e2e/fixtures/autotrack.html?tab=2');

browser.switchTab(backgroundTab);
browser.execute(ga.run, 'create', DEFAULT_TRACKER_FIELDS);
browser.execute(ga.logHitData, testId);
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);
const backgroundTab = openNewTabInBackground('/test/e2e/fixtures' +
'/page-visibility-tracker-pageload.html?testId=' + testId);

browser.waitUntil(log.hitCountEquals(2));

// The `switchTab()` command alone don't switch focus to the newly opened
// background tab, so we have to display an alert to force it.
browser.execute(() => {
alert('focus');
});
browser.pause(500);
browser.alertAccept();
browser.switchTab(backgroundTab);

browser.waitUntil(log.hitCountEquals(4));

const hits = log.getHits();
assert(hits[0].dl.endsWith('tab=1'));
assert.strictEqual(hits[0].t, 'pageview');
assert.strictEqual(hits[0].cm1, '1');
assert(hits[1].dl.endsWith('tab=2'));
assert(hits[1].dl.includes('page-visibility-tracker-pageload.html'));
assert.strictEqual(hits[1].ec, 'Page Visibility');
assert.strictEqual(hits[1].ea, 'page load');
assert.strictEqual(hits[1].ni, '1');
Expand All @@ -383,7 +367,7 @@ describe('pageVisibilityTracker', function() {
assert.strictEqual(hits[2].ec, 'Page Visibility');
assert.strictEqual(hits[2].ea, 'track');
assert(hits[2].ev > 0);
assert(hits[3].dl.endsWith('tab=2'));
assert(hits[3].dl.includes('page-visibility-tracker-pageload.html'));
assert.strictEqual(hits[3].t, 'pageview');
assert(!hits[3].cm1);
});
Expand All @@ -400,25 +384,17 @@ describe('pageVisibilityTracker', function() {
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);
browser.pause(500);

const backgroundTab = openNewTabInBackground(
'/test/e2e/fixtures/autotrack.html?tab=2');
const backgroundTab = openNewTabInBackground('/test/e2e/fixtures' +
'/page-visibility-tracker-pageload.html?testId=' + testId);

browser.switchTab(backgroundTab);
browser.execute(ga.run, 'create', DEFAULT_TRACKER_FIELDS);
browser.execute(ga.logHitData, testId);
browser.execute(ga.run, 'require', 'pageVisibilityTracker', opts);

browser.waitUntil(log.hitCountEquals(2));
expireSession();

// The `switchTab()` command alone don't switch focus to the newly opened
// background tab, so we have to display an alert to force it.
browser.execute(() => {
alert('focus');
});
browser.pause(500);
browser.alertAccept();

browser.switchTab(backgroundTab);
browser.waitUntil(log.hitCountEquals(3));

const hits = log.getHits();
Expand All @@ -428,12 +404,12 @@ describe('pageVisibilityTracker', function() {
assert(hits[0].dl.endsWith('tab=1'));
assert.strictEqual(hits[0].t, 'pageview');
assert.strictEqual(hits[0].cm1, '1');
assert(hits[1].dl.endsWith('tab=2'));
assert(hits[1].dl.includes('page-visibility-tracker-pageload.html'));
assert.strictEqual(hits[1].ec, 'Page Visibility');
assert.strictEqual(hits[1].ea, 'page load');
assert.strictEqual(hits[1].ni, '1');
assert.strictEqual(hits[1].cm1, '1');
assert(hits[2].dl.endsWith('tab=2'));
assert(hits[2].dl.includes('page-visibility-tracker-pageload.html'));
assert.strictEqual(hits[2].t, 'pageview');
assert(!hits[2].cm1);
});
Expand Down Expand Up @@ -931,7 +907,8 @@ function openNewTab(url) {
*/
function openNewTabInBackground(url) {
const browserCaps = browser.session().value;
const cmdKey = browserCaps.platform == 'MAC' ? '\uE03D' : '\uE009';
const cmdKey = browserCaps.platform.toLowerCase().startsWith('mac') ?
'\uE03D' : '\uE009';

const oldTabIds = browser.getTabIds();
browser.execute((url) => {
Expand Down

0 comments on commit e7d7121

Please sign in to comment.