Skip to content

Commit

Permalink
Automated functional tests for longform testpages (#3659)
Browse files Browse the repository at this point in the history
* initial changes for longform e2e testing

* added old safari browsers back, test spec file, and updated test pages

* add retries settings to e2e tests, lower timeout thresholds

* remove commented code

* additional clean-up

* update browser versions for more stable e2e tests

* support host param in gulp command and other updates

* refactor how host param was handled

* add ie11 to browsers but remove it for e2e tests

* update gulp task name to e2e-test

* update e2e variable
  • Loading branch information
jsnellbaker authored and jaiminpanchal27 committed Apr 23, 2019
1 parent b491a05 commit 49afe63
Show file tree
Hide file tree
Showing 32 changed files with 2,197 additions and 2,766 deletions.
40 changes: 24 additions & 16 deletions browsers.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"bs_ie_14_windows_10": {
"bs_edge_16_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "edge",
"browser_version": "14.0",
"browser_version": "16.0",
"device": null,
"os": "Windows"
},
"bs_edge_17_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "edge",
"browser_version": "17.0",
"device": null,
"os": "Windows"
},
Expand All @@ -15,51 +23,51 @@
"device": null,
"os": "Windows"
},
"bs_chrome_62_windows_10": {
"bs_chrome_72_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "chrome",
"browser_version": "62.0",
"browser_version": "72.0",
"device": null,
"os": "Windows"
},
"bs_chrome_61_windows_10": {
"bs_chrome_71_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "chrome",
"browser_version": "61.0",
"browser_version": "71.0",
"device": null,
"os": "Windows"
},
"bs_firefox_58_windows_10": {
"bs_firefox_65_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "firefox",
"browser_version": "58.0",
"browser_version": "65.0",
"device": null,
"os": "Windows"
},
"bs_firefox_57_windows_10": {
"bs_firefox_64_windows_10": {
"base": "BrowserStack",
"os_version": "10",
"browser": "firefox",
"browser_version": "57.0",
"browser_version": "64.0",
"device": null,
"os": "Windows"
},
"bs_safari_9.1_mac_elcapitan": {
"bs_safari_11_mac_high_sierra": {
"base": "BrowserStack",
"os_version": "El Capitan",
"os_version": "High Sierra",
"browser": "safari",
"browser_version": "9.1",
"browser_version": "11.1",
"device": null,
"os": "OS X"
},
"bs_safari_8_mac_yosemite": {
"bs_safari_12_mac_mojave": {
"base": "BrowserStack",
"os_version": "Yosemite",
"os_version": "Mojave",
"browser": "safari",
"browser_version": "8.0",
"browser_version": "12.0",
"device": null,
"os": "OS X"
}
Expand Down
78 changes: 9 additions & 69 deletions gulpHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const fs = require('fs.extra');
const path = require('path');
const argv = require('yargs').argv;
const MANIFEST = 'package.json';
const exec = require('child_process').exec;
const through = require('through2');
const _ = require('lodash');
const gutil = require('gulp-util');
Expand All @@ -13,7 +12,6 @@ const BUILD_PATH = './build/dist';
const DEV_PATH = './build/dev';
const ANALYTICS_PATH = '../analytics';


// get only subdirectories that contain package.json with 'main' property
function isModuleDirectory(filePath) {
try {
Expand All @@ -22,8 +20,7 @@ function isModuleDirectory(filePath) {
const module = require(manifestPath);
return module && module.main;
}
}
catch (error) {}
} catch (error) {}
}

module.exports = {
Expand All @@ -38,8 +35,8 @@ module.exports = {
jsonifyHTML: function (str) {
console.log(arguments);
return str.replace(/\n/g, '')
.replace(/<\//g, '<\\/')
.replace(/\/>/g, '\\/>');
.replace(/<\//g, '<\\/')
.replace(/\/>/g, '\\/>');
},
getArgModules() {
var modules = (argv.modules || '').split(',').filter(module => !!module);
Expand All @@ -52,7 +49,7 @@ module.exports = {
fs.readFileSync(moduleFile, 'utf8')
);
}
} catch(e) {
} catch (e) {
throw new gutil.PluginError({
plugin: 'modules',
message: 'failed reading: ' + argv.modules
Expand All @@ -72,19 +69,19 @@ module.exports = {
var moduleName = file.split(new RegExp('[.\\' + path.sep + ']'))[0];
var modulePath = path.join(absoluteModulePath, file);
if (fs.lstatSync(modulePath).isDirectory()) {
modulePath = path.join(modulePath, "index.js")
modulePath = path.join(modulePath, 'index.js')
}
memo[modulePath] = moduleName;
return memo;
}, {});
} catch(err) {
} catch (err) {
internalModules = {};
}
return Object.assign(externalModules.reduce((memo, module) => {
try {
var modulePath = require.resolve(module);
memo[modulePath] = module;
} catch(err) {
} catch (err) {
// do something
}
return memo;
Expand All @@ -93,7 +90,7 @@ module.exports = {

getBuiltModules: function(dev, externalModules) {
var modules = this.getModuleNames(externalModules);
if(Array.isArray(externalModules)) {
if (Array.isArray(externalModules)) {
modules = _.intersection(modules, externalModules);
}
return modules.map(name => path.join(__dirname, dev ? DEV_PATH : BUILD_PATH, name + '.js'));
Expand Down Expand Up @@ -128,7 +125,7 @@ module.exports = {
* Returns an array of source files for inclusion in build process
*/
getAnalyticsSources: function() {
if (!argv.analytics) {return [];} // empty arrays won't affect a standard build
if (!argv.analytics) { return []; } // empty arrays won't affect a standard build

const directoryContents = fs.readdirSync(ANALYTICS_PATH);
return directoryContents
Expand All @@ -155,62 +152,5 @@ module.exports = {
}

return options;
},

createEnd2EndTestReport : function(targetDestinationDir) {
var browsers = require('./browsers.json');
var env = [];
var input = 'bs';
for(var key in browsers) {
if(key.substring(0, input.length) === input && browsers[key].browser !== 'iphone') {
env.push(key);
}
}

//create new directory structure
fs.rmrfSync(targetDestinationDir);
env.forEach(item => {
fs.mkdirpSync(targetDestinationDir + '/' + item);
});

//move xml files to newly created directory
var walker = fs.walk('./build/coverage/e2e/reports');
walker.on("file", function (root, stat, next) {
env.forEach(item => {
if(stat.name.search(item) !== -1) {
var src = root + '/' + stat.name;
var dest = targetDestinationDir + '/' + item + '/' + stat.name;
fs.copy(src, dest, {replace: true}, function(err) {
if(err) {
throw err;
}
});
}
});
next();
});

//run junit-viewer to read xml and create html
env.forEach(item => {
//junit-viewer --results="./custom-reports/chrome51" --save="./chrome.html"
var cmd = 'junit-viewer --results="' + targetDestinationDir + '/' + item + '" --save="' + targetDestinationDir + '/' + item +'.html"';
exec(cmd);
});

//create e2e-results.html
var html = '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>End to End Testing Result</title><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"><script src="https://code.jquery.com/jquery-1.12.4.js"></script><script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script><script>$( function() {$( "#tabs" ).tabs({heightStyle: "fill"});});</script></head><body><div style="font-weight: bold;">Note: Refresh in 2-3 seconds if it says "Cannot get ....."</div><div id="tabs" style="height:2000px;">';
var li = '';
var tabs = '';
env.forEach(function(item,i) {
i++;
li = li + '<li><a href="#tabs-'+i+'">'+item+'</a></li>';
tabs = tabs + '<div id="tabs-'+i+'"><iframe name="'+item+'" src="/' + targetDestinationDir.slice(2) + '/'+item+'.html" frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;top:50px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe></div>';
});
html = html + '<ul>' + li + '</ul>' + tabs;
html = html + '</div></body></html>';

var filepath = targetDestinationDir + '/results.html';
fs.openSync(filepath, 'w+');
fs.writeFileSync(filepath, html);
}
};
77 changes: 27 additions & 50 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var fs = require('fs');
var jsEscape = require('gulp-js-escape');
const path = require('path');
const execa = require('execa');

var prebid = require('./package.json');
var dateString = 'Updated : ' + (new Date()).toISOString().substring(0, 10);
Expand All @@ -51,22 +53,6 @@ function clean() {
.pipe(gulpClean());
}

function e2etestReport() {
var reportPort = 9010;
var targetDestinationDir = './e2etest-report';
helpers.createEnd2EndTestReport(targetDestinationDir);
connect.server({
port: reportPort,
root: './',
livereload: true
});

setTimeout(function() {
opens('http://localhost:' + reportPort + '/' + targetDestinationDir.slice(2) + '/results.html');
}, 5000);
};
e2etestReport.displayName = 'e2etest-report';

// Dependant task for building postbid. It escapes postbid-config file.
function escapePostbidConfig() {
gulp.src('./integrationExamples/postbid/oas/postbid-config.js')
Expand All @@ -92,13 +78,14 @@ function lint(done) {
// View the code coverage report in the browser.
function viewCoverage(done) {
var coveragePort = 1999;
var mylocalhost = (argv.host) ? argv.host : 'localhost';

connect.server({
port: coveragePort,
root: 'build/coverage/karma_html',
livereload: false
});
opens('http://localhost:' + coveragePort);
opens('http://' + mylocalhost + ':' + coveragePort);
done();
};

Expand Down Expand Up @@ -244,6 +231,13 @@ function newKarmaCallback(done) {
function test(done) {
if (argv.notest) {
done();
} else if (argv.e2e) {
let wdioCmd = path.join(__dirname, 'node_modules/.bin/wdio');
let wdioConf = path.join(__dirname, 'wdio.conf.js');
let wdioOpts = [
wdioConf
];
return execa(wdioCmd, wdioOpts, { stdio: 'inherit' });
} else {
var karmaConf = karmaConfMaker(false, argv.browserstack, argv.watch, argv.file);

Expand All @@ -268,35 +262,6 @@ function coveralls() { // 2nd arg is a dependency: 'test' must be finished
.pipe(shell('cat build/coverage/lcov.info | node_modules/coveralls/bin/coveralls.js'));
}

function e2eTest() {
var cmdQueue = [];
if (argv.browserstack) {
var browsers = require('./browsers.json');
delete browsers['bs_ie_9_windows_7'];

var cmdStr = ' --config nightwatch.conf.js';
if (argv.group) {
cmdStr = cmdStr + ' --group ' + argv.group;
}
cmdStr = cmdStr + ' --reporter ./test/spec/e2e/custom-reporter/pbjs-html-reporter.js';

var startWith = 'bs';

Object.keys(browsers).filter(function(v) {
return v.substring(0, startWith.length) === startWith && browsers[v].browser !== 'iphone';
}).map(function(v, i, arr) {
var newArr = (i % 2 === 0) ? arr.slice(i, i + 2) : null;
if (newArr) {
var cmd = 'nightwatch --env ' + newArr.join(',') + cmdStr;
cmdQueue.push(cmd);
}
});
}

return gulp.src('')
.pipe(shell(cmdQueue.join(';')));
}

// This task creates postbid.js. Postbid setup is different from prebid.js
// More info can be found here http://prebid.org/overview/what-is-post-bid.html

Expand All @@ -308,6 +273,21 @@ function buildPostbid() {
.pipe(gulp.dest('build/postbid/'));
}

function setupE2e(done) {
if (!argv.host) {
throw new gutil.PluginError({
plugin: 'E2E test',
message: gutil.colors.red('Host should be defined e.g. ap.localhost, anlocalhost. localhost cannot be used as safari browserstack is not able to connect to localhost')
});
}
process.env.TEST_SERVER_HOST = argv.host;
if (argv.https) {
process.env.TEST_SERVER_PROTOCOL = argv.https;
}
argv.e2e = true;
done();
}

// support tasks
gulp.task(lint);
gulp.task(watch);
Expand All @@ -333,12 +313,9 @@ gulp.task('build-postbid', gulp.series(escapePostbidConfig, buildPostbid));
gulp.task('serve', gulp.series(clean, lint, gulp.parallel('build-bundle-dev', watch, test)));
gulp.task('default', gulp.series(clean, makeWebpackPkg));

gulp.task(e2etestReport);
gulp.task('e2etest', gulp.series(clean, gulp.parallel(makeDevpackPkg, makeWebpackPkg), e2eTest));

gulp.task('e2e-test', gulp.series(clean, setupE2e, gulp.parallel('build-bundle-dev', watch), test))
// other tasks
gulp.task(bundleToStdout);
gulp.task('bundle', gulpBundle.bind(null, false)); // used for just concatenating pre-built files with no build step
gulp.task('serve-nw', gulp.parallel(lint, watch, 'e2etest'));

module.exports = nodeBundle;
Loading

0 comments on commit 49afe63

Please sign in to comment.