Skip to content

Commit 4767624

Browse files
committed
feat: Allow file numbering independetly in snapshot subdirectories
1 parent fa743c6 commit 4767624

File tree

4 files changed

+101
-66
lines changed

4 files changed

+101
-66
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ module.exports = function (grunt) {
125125
},
126126
{
127127
options: {
128-
doctype: ''
128+
doctype: '',
129+
fileNumbering: 'per-directory'
129130
},
130131
url: 'http://localhost:8881/test/pages/no-doctype.html',
131132
file: 'directory/no-doctype'

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ Default value: null
176176
Destination directory to write the viewport screenshots to. It will be created if it does not exist. Default value is `null`, which means, that no screenshots will be written out.
177177

178178
#### fileNumbering
179-
Type: `Booolean`
179+
Type: `Booolean`|`String`
180180
Default value: `false`
181181

182-
Enables prefixing names of snapshot and screenshot files with their index number, for example: "002.google-input.html".
182+
If set to `true`, enables prefixing names of snapshot and screenshot files with their index number, for example: "002.google-input.html". Every occurrence of the `file` instruction increases the file count.
183+
184+
If set to "per-directory", the index number will be increased separately per sub-directory with snapshots. If the value of the `file` instruction contains a slash, it will put the snapshot to a sub-directory, which may be useful to organize many snapshots created by multiple tasks.
183185

184186
#### fileNumberDigits
185187
Type: `Number`
@@ -506,6 +508,7 @@ your code using Grunt.
506508
507509
## Release History
508510
511+
* 2018-05-14 [v1.3.0] Allow saving snapshots to sub-directories, file numbering per-directory
509512
* 2018-05-11 [v1.2.0] Introduce delay after every instruction to be able to visually follow the actions when debugging
510513
* 2018-03-29 [v1.1.0] Allow specifying all initialization parameters supported by WebdriverIO
511514
* 2018-03-28 [v1.0.2] Stop Selenium and Chromedriver processes on unexpected Grunt process abortion
@@ -548,6 +551,7 @@ Licensed under the MIT license.
548551
[grunt-reg-viz]: https://github.com/prantlf/grunt-reg-viz
549552
[grunt-selenium-standalone]: https://github.com/zs-zs/grunt-selenium-standalone
550553
[keyboard key identifiers]: https://w3c.github.io/webdriver/webdriver-spec.html#keyboard-actions
554+
[v1.3.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v1.3.0
551555
[v1.2.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v1.2.0
552556
[v1.1.0]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v1.1.0
553557
[v1.0.2]: https://github.com/prantlf/grunt-html-dom-snapshot/releases/tag/v1.0.2

tasks/html-dom-snapshot.js

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const {writeFile} = require('fs')
1313
const pad = require('pad-left')
14-
const {dirname, isAbsolute, join} = require('path')
14+
const {basename, dirname, isAbsolute, join} = require('path')
1515
const mkdirp = require('mkdirp')
1616
const nodeCleanup = require('node-cleanup')
1717
const instructions = [
@@ -24,6 +24,7 @@ const instructions = [
2424
'isNotFocused', 'isNotSelected', 'isNotVisible',
2525
'isNotVisibleWithinViewport', 'abort'
2626
].map(instruction => require('./instructions/' + instruction))
27+
const directoryCounts = {}
2728
let fileCount = 0
2829

2930
module.exports = grunt => {
@@ -138,7 +139,7 @@ module.exports = grunt => {
138139
.reduce((scenarios, scenario) =>
139140
scenarios.concat(grunt.file.expand(scenario)), [])
140141
.reduce((scenarios, scenario) => {
141-
grunt.log.ok('Load scenario "' + scenario + '".')
142+
grunt.verbose.writeln('Load scenario "' + scenario + '".')
142143
if (!isAbsolute(scenario)) {
143144
scenario = join(currentDirectory, scenario)
144145
}
@@ -226,18 +227,20 @@ module.exports = grunt => {
226227
}
227228
}), viewportSet)
228229
.then(() => {
229-
if (snapshots && screenshots) {
230-
++fileCount
231-
return performInstruction(
232-
Promise.all([makeSnapshot(), makeScreenshot()]))
233-
}
234-
if (snapshots) {
235-
++fileCount
236-
return performInstruction(makeSnapshot())
237-
}
238-
if (screenshots) {
239-
++fileCount
240-
return performInstruction(makeScreenshot())
230+
if (file) {
231+
if (snapshots && screenshots) {
232+
increaseFileCount(file)
233+
return performInstruction(
234+
Promise.all([makeSnapshot(), makeScreenshot()]))
235+
}
236+
if (snapshots) {
237+
increaseFileCount(file)
238+
return performInstruction(makeSnapshot())
239+
}
240+
if (screenshots) {
241+
increaseFileCount(file)
242+
return performInstruction(makeScreenshot())
243+
}
241244
}
242245
})
243246

@@ -260,62 +263,89 @@ module.exports = grunt => {
260263
}
261264

262265
function saveContent (html) {
263-
if (file) {
264-
let fileName = file.toLowerCase()
265-
fileName = fileName.endsWith('.html') ||
266-
fileName.endsWith('.htm') ? file : file + '.html'
267-
if (fileNumbering) {
268-
fileName = numberFileName(fileName)
269-
}
270-
fileName = join(snapshots, fileName)
271-
grunt.log.ok('Write snapshot to "' + fileName + '".')
272-
const directory = dirname(fileName)
273-
return ensureDirectory(directory)
274-
.then(() => new Promise((resolve, reject) =>
275-
writeFile(fileName, commandOptions.doctype + html,
276-
error => {
277-
if (error) {
278-
reject(error)
279-
} else {
280-
++snapshotCount
281-
resolve()
282-
}
283-
})
284-
))
266+
let fileName = file.toLowerCase()
267+
fileName = fileName.endsWith('.html') ||
268+
fileName.endsWith('.htm') ? file : file + '.html'
269+
if (fileNumbering) {
270+
fileName = numberFileName(fileName, fileNumbering)
285271
}
272+
fileName = join(snapshots, fileName)
273+
grunt.log.ok('Write snapshot to "' + fileName + '".')
274+
const directory = dirname(fileName)
275+
return ensureDirectory(directory)
276+
.then(() => new Promise((resolve, reject) =>
277+
writeFile(fileName, commandOptions.doctype + html,
278+
error => {
279+
if (error) {
280+
reject(error)
281+
} else {
282+
++snapshotCount
283+
resolve()
284+
}
285+
})
286+
))
286287
}
287288

288289
function saveImage (png) {
289-
if (file) {
290-
let fileName = file.toLowerCase()
291-
fileName = fileName.endsWith('.html')
292-
? file.substr(0, file.length - 5)
293-
: fileName.endsWith('.htm')
294-
? file.substr(0, file.length - 4) : file
295-
if (fileNumbering) {
296-
fileName = numberFileName(fileName)
290+
let fileName = file.toLowerCase()
291+
fileName = fileName.endsWith('.html')
292+
? file.substr(0, file.length - 5)
293+
: fileName.endsWith('.htm')
294+
? file.substr(0, file.length - 4) : file
295+
if (fileNumbering) {
296+
fileName = numberFileName(fileName, fileNumbering)
297+
}
298+
fileName = join(screenshots, fileName + '.png')
299+
grunt.log.ok('Write screenshot to "' + fileName + '".')
300+
const directory = dirname(fileName)
301+
return ensureDirectory(directory)
302+
.then(() => new Promise((resolve, reject) =>
303+
writeFile(fileName, Buffer.from(png.value, 'base64'),
304+
error => {
305+
if (error) {
306+
reject(error)
307+
} else {
308+
++screenshotCount
309+
resolve()
310+
}
311+
})
312+
))
313+
}
314+
315+
function increaseFileCount (file) {
316+
++fileCount
317+
if (file.indexOf('/') > 0) {
318+
const directory = dirname(file)
319+
let directoryCount = directoryCounts[directory]
320+
if (directoryCount) {
321+
directoryCounts[directory] = directoryCount + 1
322+
} else {
323+
directoryCounts[directory] = 1
297324
}
298-
fileName = join(screenshots, fileName + '.png')
299-
grunt.log.ok('Write screenshot to "' + fileName + '".')
300-
const directory = dirname(fileName)
301-
return ensureDirectory(directory)
302-
.then(() => new Promise((resolve, reject) =>
303-
writeFile(fileName, Buffer.from(png.value, 'base64'),
304-
error => {
305-
if (error) {
306-
reject(error)
307-
} else {
308-
++screenshotCount
309-
resolve()
310-
}
311-
})
312-
))
313325
}
314326
}
315327

316-
function numberFileName (fileName) {
317-
return pad(fileCount.toString(), fileNumberDigits, '0') +
318-
fileNumberSeparator + fileName
328+
function numberFileName (fileName, fileNumbering) {
329+
let directory, number
330+
if (fileName.indexOf('/') > 0) {
331+
directory = dirname(fileName)
332+
if (fileNumbering === true) {
333+
number = fileCount
334+
} else {
335+
number = directoryCounts[directory]
336+
}
337+
} else {
338+
number = fileCount
339+
}
340+
if (directory) {
341+
fileName = basename(fileName)
342+
}
343+
fileName = pad(number.toString(), fileNumberDigits, '0') +
344+
fileNumberSeparator + fileName
345+
if (directory) {
346+
fileName = join(directory, fileName)
347+
}
348+
return fileName
319349
}
320350
}
321351
})

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exports['html-dom-snapshot'] = {
6868
},
6969

7070
'no-doctype': function (test) {
71-
const pages = readPages('no-doctype.html', 'directory/no-doctype.html')
71+
const pages = readPages('no-doctype.html', 'directory/001.no-doctype.html')
7272
test.equal(pages.expected, pages.actual, 'no-doctype.html')
7373
test.done()
7474
},

0 commit comments

Comments
 (0)