Skip to content

Commit

Permalink
Merge pull request #204 from gauntface/test-fix
Browse files Browse the repository at this point in the history
Bring the tests back to life
  • Loading branch information
marco-c authored Aug 26, 2016
2 parents f9b8d32 + 4b42e42 commit 1d47bd0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 95 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"devDependencies": {
"chalk": "^1.1.3",
"chromedriver": "^2.21.2",
"chromedriver": "^2.23.1",
"del": "^2.2.1",
"dmg": "^0.1.0",
"eslint": "^2.10.2",
Expand All @@ -54,11 +54,10 @@
"mocha": "^2.4.5",
"portfinder": "^1.0.2",
"request": "^2.69.0",
"selenium-assistant": "0.3.0",
"selenium-assistant": "0.4.0",
"selenium-webdriver": "~2.53.2",
"semver": "^5.1.0",
"temp": "^0.8.3",
"which": "^1.2.9"
"temp": "^0.8.3"
},
"engines": {
"node": ">= v0.10.0"
Expand Down
44 changes: 35 additions & 9 deletions test/helpers/download-test-browsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,50 @@
const invalidNodeVersions = /0.(10|12).(\d+)/;
if (process.versions.node.match(invalidNodeVersions)) {
console.log('Skipping downloading browsers as selenium tests can\'t run on ' + process.versions.node);
return;
return null;
}

/* eslint-disable global-require*/
const seleniumAssistant = require('selenium-assistant');
/* eslint-enable global-require*/

let forceDownload = false;
if (process.env.TRAVIS) {
forceDownload = true;
}

const promises = [
seleniumAssistant.downloadFirefoxDriver(),
seleniumAssistant.downloadBrowser('firefox', 'stable'),
seleniumAssistant.downloadBrowser('firefox', 'beta'),
seleniumAssistant.downloadBrowser('firefox', 'unstable'),
seleniumAssistant.downloadBrowser('chrome', 'stable'),
seleniumAssistant.downloadBrowser('chrome', 'beta'),
seleniumAssistant.downloadBrowser('chrome', 'unstable')
seleniumAssistant.downloadFirefoxDriver()
.catch(function(err) {
console.error('Firefox Driver Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('firefox', 'stable', forceDownload)
.catch(function(err) {
console.error('Firefox Stable Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('firefox', 'beta', forceDownload)
.catch(function(err) {
console.error('Firefox Beta Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('firefox', 'unstable', forceDownload)
.catch(function(err) {
console.error('Firefox Unstable Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('chrome', 'stable', forceDownload)
.catch(function(err) {
console.error('Chrome Stable Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('chrome', 'beta', forceDownload)
.catch(function(err) {
console.error('Chrome Beta Download Error: ', err);
}),
seleniumAssistant.downloadBrowser('chrome', 'unstable', forceDownload)
.catch(function(err) {
console.error('Chrome Unstable Download Error: ', err);
})
];

Promise.all(promises)
return Promise.all(promises)
.then(function() {
console.log('Download complete.');
});
Expand Down
170 changes: 88 additions & 82 deletions test/testSelenium.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
options = options || {};

if (browser.getSeleniumBrowserId() === 'firefox' &&
browser.getVersionNumber() <= 48 &&
process.env.TRAVIS === 'true') {
console.log('');
console.warn(chalk.red(
Expand All @@ -49,6 +50,20 @@
return Promise.resolve();
}

// Works locally, but on Travis breaks. Probably best to wait for next
// ChromeDriver release.
if (browser.getSeleniumBrowserId() === 'chrome' &&
browser.getVersionNumber() === 54 &&
process.env.TRAVIS === 'true') {
console.log('');
console.warn(chalk.red(
'Running on Travis so skipping Chrome V54 tests as ' +
'they don\'t currently work.'
));
console.log('');
return Promise.resolve();
}

return createServer(options, webPush)
.then(function(server) {
globalServer = server;
Expand Down Expand Up @@ -87,6 +102,7 @@
const seleniumOptions = browser.getSeleniumOptions();
seleniumOptions.addArguments('user-data-dir=' + tempPreferenceDir + '/');
}

return browser.getSeleniumDriver();
})
.then(function(driver) {
Expand All @@ -96,97 +112,87 @@
testServerURL += '?vapid=' + urlBase64.encode(options.vapid.publicKey);
}

// Tests will likely expect a native promise with then and catch
// Not the web driver promise of then and thenCatch
return new Promise(function(resolve, reject) {
globalDriver.get(testServerURL)
.then(function() {
return globalDriver.executeScript(function() {
return typeof navigator.serviceWorker !== 'undefined';
});
})
.then(function(serviceWorkerSupported) {
assert(serviceWorkerSupported);
})
.then(function() {
return globalDriver.wait(function() {
return globalDriver.executeScript(function() {
return typeof window.subscribeSuccess !== 'undefined';
});
});
})
.then(function() {
return globalDriver.get(testServerURL)
.then(function() {
return globalDriver.executeScript(function() {
return typeof navigator.serviceWorker !== 'undefined';
});
})
.then(function(serviceWorkerSupported) {
assert(serviceWorkerSupported);
})
.then(function() {
return globalDriver.wait(function() {
return globalDriver.executeScript(function() {
if (!window.subscribeSuccess) {
return window.subscribeError;
}

return null;
return typeof window.subscribeSuccess !== 'undefined';
});
})
.then(function(subscribeError) {
if (subscribeError) {
console.log('subscribeError: ', subscribeError);
throw subscribeError;
});
})
.then(function() {
return globalDriver.executeScript(function() {
if (!window.subscribeSuccess) {
return window.subscribeError;
}

return globalDriver.executeScript(function() {
return window.testSubscription;
return null;
});
})
.then(function(subscribeError) {
if (subscribeError) {
console.log('subscribeError: ', subscribeError);
throw subscribeError;
}

return globalDriver.executeScript(function() {
return window.testSubscription;
});
})
.then(function(subscription) {
if (!subscription) {
throw new Error('No subscription found.');
}

subscription = JSON.parse(subscription);

let promise;
let pushPayload = null;
let vapid = null;
if (options) {
pushPayload = options.payload;
vapid = options.vapid;
}

if (!pushPayload) {
promise = webPush.sendNotification(subscription.endpoint, {
vapid: vapid
});
})
.then(function(subscription) {
if (!subscription) {
throw new Error('No subscription found.');
}

subscription = JSON.parse(subscription);

let promise;
let pushPayload = null;
let vapid = null;
if (options) {
pushPayload = options.payload;
vapid = options.vapid;
} else {
if (!subscription.keys) {
throw new Error('Require subscription.keys not found.');
}

if (!pushPayload) {
promise = webPush.sendNotification(subscription.endpoint, {
vapid: vapid
});
} else {
if (!subscription.keys) {
throw new Error('Require subscription.keys not found.');
promise = webPush.sendNotification(subscription.endpoint, {
payload: pushPayload,
userPublicKey: subscription.keys.p256dh,
userAuth: subscription.keys.auth,
vapid: vapid
});
}

return promise
.then(function(response) {
if (response.length > 0) {
const data = JSON.parse(response);
if (typeof data.failure !== 'undefined' && data.failure > 0) {
throw new Error('Bad GCM Response: ' + response);
}

promise = webPush.sendNotification(subscription.endpoint, {
payload: pushPayload,
userPublicKey: subscription.keys.p256dh,
userAuth: subscription.keys.auth,
vapid: vapid
});
}

return promise
.then(function(response) {
if (response.length > 0) {
const data = JSON.parse(response);
if (typeof data.failure !== 'undefined' && data.failure > 0) {
throw new Error('Bad GCM Response: ' + response);
}
}
});
})
.then(function() {
const expectedTitle = options.payload ? options.payload : 'no payload';
return globalDriver.wait(function() {
return webdriver.until.titleIs(expectedTitle, 60000);
});
})
.then(function() {
resolve();
})
.thenCatch(function(err) {
reject(err);
});
})
.then(function() {
const expectedTitle = options.payload ? options.payload : 'no payload';
return globalDriver.wait(function() {
return webdriver.until.titleIs(expectedTitle, 60000);
});
});
});
Expand Down

0 comments on commit 1d47bd0

Please sign in to comment.