[functional testing] Load webdriver

This commit is contained in:
Jonathan Budzenski 2015-09-04 14:11:28 -05:00
parent 83a582cdbe
commit 71dd15ca22
3 changed files with 89 additions and 3 deletions

View file

@ -6,16 +6,17 @@ var colors = require('ansicolors');
module.exports = function (grunt) {
grunt.registerTask('loadFixtures', 'Loads fixtures into elasticsearch', function () {
const FIXTURES_PATH = path.join(grunt.config.get('root'), 'test/fixtures');
const config = this.options();
const done = this.async();
const fixturesPath = path.join(grunt.config.get('root'), 'test/fixtures');
fs.readdir(fixturesPath, function (err, files) {
fs.readdir(FIXTURES_PATH, function (err, files) {
if (err) grunt.fail.warn(err);
let doneProcessing = 0;
files.forEach(function (file) {
fs.createReadStream(path.join(fixturesPath, file))
fs.createReadStream(path.join(FIXTURES_PATH, file))
.pipe(request.post(`${config.server}/_bulk`, function (err, res, body) {
if (err || res.statusCode !== 200) grunt.fail.warn(err || body);
grunt.log.write(`[${colors.green('success')}] ${file}`);

84
tasks/startSelenium.js Normal file
View file

@ -0,0 +1,84 @@
var _ = require('lodash');
var request = require('request');
var fs = require('fs');
var path = require('path');
var colors = require('ansicolors');
var crypto = require('crypto');
var {spawn} = require('child_process');
module.exports = function (grunt) {
grunt.registerTask('startSelenium', 'Start an instance of selenium standalone', function () {
const done = this.async();
const config = _.defaults(this.options(), {
seleniumStandalone: {
filename: 'selenium-server-standalone-2.47.1.jar',
server: 'https://selenium-release.storage.googleapis.com/2.47/',
md5: 'e6cb10b8f0f353c6ca4a8f62fb5cb472'
}
});
const SELENIUM_DIRECTORY = path.join(grunt.config.get('root'), 'selenium');
const SELENIUM_FILE_PATH = path.join(SELENIUM_DIRECTORY, config.seleniumStandalone.filename);
const SELENIUM_DOWNLOAD_URL = config.seleniumStandalone.server + config.seleniumStandalone.filename;
const SELENIUM_HASH = config.seleniumStandalone.md5;
function waitForReadiness(seleniumProcess, ready) {
seleniumProcess.stderr.on('data', function (log) {
if (~log.toString('utf8').indexOf('Selenium Server is up and running')) {
grunt.log.writeln('Selenium standalone started on port 4444');
done();
}
});
process.on('exit', function () {
seleniumProcess.kill();
});
}
function validateDownload(path, expectedHash, success) {
grunt.log.write('Validating hash...');
fs.readFile(path, function (err, data) {
if (err) grunt.fail.warn(err);
const calculatedHash = crypto.createHash('md5').update(data).digest('hex');
if (calculatedHash !== expectedHash) return grunt.fail.warn('Selenium download has an invalid hash');
grunt.log.writeln('done');
success();
});
}
function spawnSelenium() {
var seleniumProcess = spawn('java', ['-jar', SELENIUM_FILE_PATH]);
waitForReadiness(seleniumProcess);
}
function downloadSelenium(success) {
grunt.log.write(`Downloading ${SELENIUM_DOWNLOAD_URL}...`);
request.get(SELENIUM_DOWNLOAD_URL)
.pipe(fs.createWriteStream(SELENIUM_FILE_PATH))
.on('finish', function () {
grunt.log.writeln('done');
validateDownload(SELENIUM_FILE_PATH, SELENIUM_HASH, success);
})
.on('error', function (err) {
grunt.fail.warn(err);
});
}
function start() {
fs.mkdir(SELENIUM_DIRECTORY, function (err) {
if (err && err.code !== 'EEXIST') grunt.fail.warn(err);
fs.exists(SELENIUM_FILE_PATH, function (exists) {
if (exists) {
spawnSelenium();
} else {
downloadSelenium(spawnSelenium);
}
});
});
}
start();
});
};

View file

@ -17,6 +17,7 @@ module.exports = function (grunt) {
grunt.registerTask('test:ui', [
'esvm:ui',
'loadFixtures',
'startSelenium',
'run:testUIServer',
'intern:dev'
]);