Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ That's all, explore the generated report and enjoy!
<img width="479" height="347" src="screenshot.png">
</p>

## 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]])`
Expand Down
11 changes: 6 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions utils/parseSnapshotOptions.js
Original file line number Diff line number Diff line change
@@ -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 };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if this works, you don't need to reassign the accumulator, only extend it, e.g.

+acc.quality = parseInt(cur.quality, 10);
-cur.quality = parseInt(cur.quality, 10);
-acc = { ...acc, ...cur };

the same happen with all options, and the code will be less 🐝

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense 👌🏻

} 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;
}, {}),
};
};