diff --git a/README.md b/README.md index 272a368..4683a61 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,21 @@ That's all, explore the generated report and enjoy!

+## CLI Options (since v0.6.x) + +- `as` the identifier for the later comparison +- [`quality`] by default snapshot are taken as png in full resolution - use this options to use jpeg compression. +- [`mw`] max width boundary - resize the screenshots that are larger than this value +- [`mh`] max height boundary - resize the screenshots that are larger than this value + +### Usage + +- `--take-snapshot base` +- `--take-snapshot as=base` +- `--take-snapshot as=base,quality=75` +- `--take-snapshot as=base,mw=1200` +- `--take-snapshot as=base,mw=800,mh=600,quality=90` + ## API Options `takeSnapshot(t[, label[, options]])` diff --git a/lib/index.js b/lib/index.js index 5250919..8a70085 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,13 +6,13 @@ require('global-or-local').devDependencies(['testcafe']); const { Selector, ClientFunction } = require('testcafe'); const path = require('path'); const fs = require('fs'); +const { parseSnapshotOptions } = require('../utils/parseSnapshotOptions'); const isFullPage = process.env.FULL_PAGE || (process.argv.slice(2).indexOf('--full-page') !== -1); const isTakeSnapshot = process.env.TAKE_SNAPSHOT || process.argv.slice(2).indexOf('--take-snapshot'); -const snapshotName = process.env.SNAPSHOT_NAME || (isTakeSnapshot > -1 ? process.argv.slice(2)[isTakeSnapshot + 1] : null); - -const type = (snapshotName && snapshotName.match(/^[a-z\d_]/) ? snapshotName : null) - || (isTakeSnapshot !== -1 ? 'base' : 'actual'); +const snapshotOptionsString = process.env.SNAPSHOT_NAME || (isTakeSnapshot > -1 ? process.argv.slice(2)[isTakeSnapshot + 1] : null); +const snapshotOptions = (snapshotOptionsString && parseSnapshotOptions(snapshotOptionsString)) + || (isTakeSnapshot !== -1 ? { as: 'base' } : { as: 'actual' }); function getCurrentStack() { let found; @@ -51,9 +51,10 @@ function _takeSnapshot(t, opts, extra) { } const options = Object.assign({}, opts); + const type = options.as || snapshotOptions.as; const groupId = options.base || t.testRun.test.fixture.name; const filename = normalize(options.label || t.testRun.test.name); - const imagePath = `${normalize(groupId)}/${filename}/${options.as || type}.png`; + const imagePath = `${normalize(groupId)}/${filename}/${type}.png`; const selectors = !Array.isArray(options.selector) && options.selector ? [options.selector] : options.selector; // correctly set up fullPage variable diff --git a/utils/parseSnapshotOptions.js b/utils/parseSnapshotOptions.js new file mode 100644 index 0000000..5a681e5 --- /dev/null +++ b/utils/parseSnapshotOptions.js @@ -0,0 +1,36 @@ +const OPTIONS_SEPARATOR = ','; +const KEY_VALUE_SEPARATOR = '='; +const DEFAULT_OPTIONS = { as: 'base' }; + +module.exports.parseSnapshotOptions = (optionsStr = '') => { + if (typeof optionsStr !== 'string' || optionsStr.trim() === '') { + return { ...DEFAULT_OPTIONS }; + } + const parsed = optionsStr.trim() + .split(OPTIONS_SEPARATOR) + .map(keyValueString => keyValueString.split(KEY_VALUE_SEPARATOR)) + .map(([key, value]) => { + return value && value.trim().length ? { [key]: value.trim() } : key.trim(); + }); + if (parsed.length === 1 && typeof parsed[0] === 'string') { + return parsed[0].match(/^[a-z\d_]/) ? { as: parsed[0] } : { ...DEFAULT_OPTIONS }; + } + return { + ...DEFAULT_OPTIONS, + ...parsed.reduce((acc, cur) => { + if (cur.quality && !isNaN(cur.quality)) { + cur.quality = parseInt(cur.quality, 10); + acc = { ...acc, ...cur }; + } else if (cur.mw && !isNaN(cur.mw)) { + cur.mw = parseInt(cur.mw, 10); + acc = { ...acc, ...cur }; + } else if (cur.mh && !isNaN(cur.mh)) { + cur.mh = parseInt(cur.mh, 10); + acc = { ...acc, ...cur }; + } else if (cur.as && cur.as.match(/^[a-z\d_]/)) { + acc = { ...acc, ...cur }; + } + return acc; + }, {}), + }; +};