Skip to content

Commit 8c64a86

Browse files
committed
Refactor Node App
1 parent 41998db commit 8c64a86

13 files changed

+259
-715
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Automating your QA with Visual Regression Testing Example Repository
22

3-
This repository is an example for my talk [Automating your QA with Visual Regression Testing](https://2019.europe.wordcamp.org/session/automating-your-qa-with-visual-regression-testing/) at WordCamp Europe 2019. It uses [BackstopJS](https://github.com/garris/BackstopJS/) and [Node JS](https://nodejs.org/) to automate visual QA. The slides for the talk can be found [here](https://goo.gl/V7QtNw).
3+
This repository is an example for my talk [Automating your QA with Visual Regression Testing](https://2019.europe.wordcamp.org/session/automating-your-qa-with-visual-regression-testing/) at WordCamp Europe 2019. The slides for the talk can be found [here](https://goo.gl/V7QtNw).
4+
5+
[BackstopJS](https://github.com/garris/BackstopJS/) is used for the visual regression testing. The app itself is built with [Node JS](https://nodejs.org/), [`commander.js`](https://github.com/tj/commander.js/), and [`Inquirer.js`](https://github.com/SBoudrias/Inquirer.js).
46

57
## Prerequisites
68

@@ -21,7 +23,7 @@ After setting up the repository locally (see above) you will need to:
2123

2224
1. Run the command `npm install` to download dependencies
2325
1. Run the command `npm run start`
24-
* Type the number of a site from the list or enter _all_ to test all sites in `src/sitesToTest.js`
26+
* Select the site you want to test from the list
2527
1. Check out the results from the sample test
2628
* They should open in your browser automatically
2729
1. Edit `src/sitesToTest.js`
@@ -34,7 +36,7 @@ After setting up the repository locally (see above) you will need to:
3436
1. Run the command `npm run build`.
3537
* This command needs to be run anytime you edit items in `src`
3638
1. Run the command `npm run start`.
37-
* To test a single site, enter its number from the list, or enter `all` to test all sites in `src/sitesToTest.js`
39+
* Select the site you want to test from the list
3840

3941
**Troubleshooting**
4042
If you are having issues with the script hanging or BackstopJS taking a long time there may be headless Chrome instances that didn't close properly.

dist/backstopConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function backstopConfig(nonProductionBaseUrl, productionBaseUrl, pathsToTest, si
3737
}],
3838
'paths': {
3939
'ci_report': `${backstopDataDir}/ci_report`,
40+
'json_report': `${backstopDataDir}/json_report`,
4041
'html_report': `${backstopDataDir}/html_report`,
4142
'bitmaps_reference': `${backstopDataDir}/bitmaps_reference`,
4243
'bitmaps_test': `${backstopDataDir}/bitmaps_test`,
@@ -45,7 +46,7 @@ function backstopConfig(nonProductionBaseUrl, productionBaseUrl, pathsToTest, si
4546
'engine_scripts': `${backstopDataDir}/engine_scripts`
4647
},
4748
'engine': 'puppeteer',
48-
'report': ['browser', 'CI'],
49+
'report': ['browser', 'json'],
4950
'casperFlags': [],
5051
'debug': false,
5152
'port': 3001

dist/index.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
1+
#!/usr/bin/env node
2+
// External dependencies
13
"use strict";
24

3-
var _visualRegressionTests = _interopRequireDefault(require("./visualRegressionTests"));
5+
var _commander = _interopRequireDefault(require("commander"));
6+
7+
var _inquirer = _interopRequireDefault(require("inquirer"));
8+
9+
var _ansiColors = _interopRequireDefault(require("ansi-colors"));
410

5-
var _testAllSites = _interopRequireDefault(require("./testAllSites"));
11+
var _visualRegressionTestSite = _interopRequireDefault(require("./visualRegressionTestSite"));
612

7-
var _minimist = _interopRequireDefault(require("minimist"));
13+
var _sitesToTest = _interopRequireDefault(require("./sitesToTest"));
14+
15+
var _throwError = _interopRequireDefault(require("./throwError"));
816

917
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1018

1119
// Local dependencies
12-
// External dependencies
13-
const args = (0, _minimist.default)(process.argv.slice(2), {}); // https://nodejs.org/api/process.html#process_process_platform
14-
// const isWindows = process.platform === "win32";
20+
// Get the site names
21+
const siteNames = Object.keys(_sitesToTest.default); // Throw an error if there are not sites defined
22+
23+
if (siteNames.length === 0) {
24+
(0, _throwError.default)(_ansiColors.default.red(`There are no sites defined in the ${_ansiColors.default.grey('sitesToTest.js')} config file`));
25+
} // Start a new program
26+
27+
28+
const program = new _commander.default.Command(); // Set the program version
29+
30+
program.version('1.0.0'); // Allow site name to be passed as an option
31+
32+
program.option('-s, --site [siteName]', 'specifiy a site to be tested'); // Process the arguments
33+
34+
program.parse(process.argv); // If a site was specified, use it
1535

16-
if (Object.prototype.hasOwnProperty.call(args, 'all')) {
17-
(0, _testAllSites.default)();
36+
if (program.site) {
37+
(0, _visualRegressionTestSite.default)(program.site);
1838
} else {
19-
(0, _visualRegressionTests.default)();
39+
// Ask which site should be used
40+
_inquirer.default.prompt([{
41+
type: 'list',
42+
name: 'site',
43+
message: 'Which site do you want to test?',
44+
choices: siteNames
45+
}]).then(answers => {
46+
if (Object.prototype.hasOwnProperty.call(answers, 'site')) {
47+
(0, _visualRegressionTestSite.default)(answers.site);
48+
}
49+
});
2050
}

dist/testAllSites.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

dist/visualRegressionTestSite.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,51 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.default = _default;
77

8+
var _backstopjs = _interopRequireDefault(require("backstopjs"));
9+
10+
var _fancyLog = _interopRequireDefault(require("fancy-log"));
11+
12+
var _ansiColors = _interopRequireDefault(require("ansi-colors"));
13+
814
var _throwError = _interopRequireDefault(require("./throwError"));
915

1016
var _backstopConfig = _interopRequireDefault(require("./backstopConfig"));
1117

1218
var _sitesToTest = _interopRequireDefault(require("./sitesToTest"));
1319

14-
var _path = _interopRequireDefault(require("path"));
20+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1521

16-
var _backstopjs = _interopRequireDefault(require("backstopjs"));
22+
// External dependencies
23+
// Local dependencies
24+
function _default(siteToTest) {
25+
// Ensure the selected site exists in the config
26+
const siteExists = Object.prototype.hasOwnProperty.call(_sitesToTest.default, siteToTest); // Throw an error if it doesn't
1727

18-
var _fancyLog = _interopRequireDefault(require("fancy-log"));
28+
if (!siteExists) {
29+
(0, _throwError.default)(_ansiColors.default.red(`${_ansiColors.default.bold(siteToTest)} is not a valid site. Check the name you entered against the ${_ansiColors.default.grey('sitesToTest.js')} config file`));
30+
} // Stash the current site
1931

20-
var _ansiColors = _interopRequireDefault(require("ansi-colors"));
2132

22-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33+
const site = _sitesToTest.default[siteToTest]; // Stash the site label
2334

24-
// Local dependencies
25-
// External dependencies
26-
function _default(siteToTest) {
27-
const siteExists = Object.prototype.hasOwnProperty.call(_sitesToTest.default, siteToTest);
35+
const boldLabel = `${_ansiColors.default.bold(`${site.label}`)}`; // Let the user know we are starting the tests
2836

29-
let rootDir = _path.default.dirname(require.main.filename);
37+
(0, _fancyLog.default)(_ansiColors.default.bgYellow(`Running visual regression tests on ${boldLabel}...\n`)); // Generate site specific configuration.
3038

31-
if (rootDir.endsWith('dist')) {
32-
rootDir = rootDir.substring(0, rootDir.indexOf('/dist'));
33-
}
39+
const currentConfig = (0, _backstopConfig.default)(site.nonProductionBaseUrl, site.productionBaseUrl, site.pathsToTest, site.name); // Disable logging since BackstopJS is noisy
40+
// console.log = function () {};
3441

35-
if (!siteExists) {
36-
(0, _throwError.default)(_ansiColors.default.red(`${_ansiColors.default.gray(siteToTest)} is not a valid site. Check the name you entered against the ${_ansiColors.default.gray('sitesToTest.js')} config file`));
37-
}
38-
39-
_sitesToTest.default[siteToTest].name = siteToTest;
40-
const site = _sitesToTest.default[siteToTest];
41-
(0, _fancyLog.default)(_ansiColors.default.yellow(`Testing: ${site.label}`));
42-
(0, _fancyLog.default)(`Generating configuration for ${site.label}`);
43-
const currentConfig = (0, _backstopConfig.default)(site.nonProductionBaseUrl, site.productionBaseUrl, site.pathsToTest, site.name);
44-
(0, _fancyLog.default)(`Running Backstop tests for ${site.label}`);
4542
(0, _backstopjs.default)('reference', {
4643
config: currentConfig
4744
}).then(() => {
48-
(0, _fancyLog.default)(`Backstop JS reference complete for ${site.label}! Starting tests.`);
4945
(0, _backstopjs.default)('test', {
5046
config: currentConfig
5147
}).then(() => {
52-
(0, _fancyLog.default)(_ansiColors.default.yellow(`Report saved to ${_ansiColors.default.gray(`${rootDir}/backstop_data/${site.name}/html_report/index.html`)}`));
53-
(0, _fancyLog.default)(_ansiColors.default.green(`Backstop JS tests passed for ${site.label}!`));
48+
(0, _fancyLog.default)(_ansiColors.default.bgGreen(`Backstop JS tests passed for ${site.label}!`));
5449
}).catch(() => {
55-
(0, _fancyLog.default)(`Report saved to "${rootDir}/backstop_data/${site.name}/html_report/index.html"`);
56-
(0, _throwError.default)(_ansiColors.default.red(`Backstop JS tests failed for ${_ansiColors.default.gray(site.label)}!`));
50+
(0, _fancyLog.default)(_ansiColors.default.bgRed(_ansiColors.default.white(`Backstop JS tests failed for ${site.label}!`)));
5751
});
5852
}).catch(() => {
59-
(0, _throwError.default)(_ansiColors.default.red(`Backstop JS reference failed for ${_ansiColors.default.gray(site.label)}! Please check the BackstopReferenceBaseUrl`));
53+
(0, _fancyLog.default)(_ansiColors.default.bgRed(_ansiColors.default.white(`Backstop JS tests failed for ${site.label}!`)));
6054
});
6155
}

dist/visualRegressionTests.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)