Skip to content

Commit 581e056

Browse files
committed
fix: Rename the property "pages" in a sub-task to "commands"
* Recognize "pages" too for compatibility. * Warn about deprecated property if "pages" is used.
1 parent a75ef0d commit 581e056

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

Gruntfile.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function (grunt) {
7171
url: 'http://localhost:8881/test/pages/single-target.html'
7272
},
7373
others: {
74-
pages: [
74+
commands: [
7575
{
7676
file: 'static.html',
7777
url: 'http://localhost:8881/test/pages/static.html'
@@ -135,17 +135,20 @@ module.exports = function (grunt) {
135135
options: {
136136
force: true
137137
},
138-
pages: [
138+
commands: [
139139
{}
140140
]
141141
},
142142
'invalid-file': {
143143
options: {
144144
force: true
145145
},
146-
'//': {
147-
url: 'http://localhost:8881'
148-
}
146+
pages: [
147+
{
148+
url: 'http://localhost:8881',
149+
file: '//'
150+
}
151+
]
149152
},
150153
'invalid-dest': {
151154
options: {

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ File names for snapshots can be used as sub-task names. Separate sub-tasks initi
149149
}
150150
```
151151

152-
If the sub-task contains a property `pages`, this property is supposed to point to an array of commands - navigations and other browser interactions, waiting for browser states and making snapshots. They share the same instance of the webdriver, which improves the performance:
152+
If the sub-task contains a property `commands`, this property is supposed to point to an array of command objects - navigations and other browser interactions, waiting for browser states and making snapshots. They share the same instance of the webdriver, which improves the performance:
153153

154154
```js
155155
'html-dom-snapshot': {
156156
all: {
157-
pages: [
157+
commands: [
158158
{
159159
file: 'google.html',
160160
url: 'https://google.com'
@@ -282,7 +282,7 @@ If `wait` is omitted, the task will advance to another item without delay. It ca
282282

283283
### Parameter Combinations
284284

285-
The following array of commands within the `pages` property will change location, make a snapshot immediately to save the server-side pre-rendered content, then another one to see the progress after the first 500 milliseconds and yet another one, once the search form is ready. Then it submits the form by clicking on the "Search" button, waits until the search results are displayed and makes one final snapshot.
285+
The following array of commands within the `commands` property will change location, make a snapshot immediately to save the server-side pre-rendered content, then another one to see the progress after the first 500 milliseconds and yet another one, once the search form is ready. Then it submits the form by clicking on the "Search" button, waits until the search results are displayed and makes one final snapshot.
286286

287287
```js
288288
{
@@ -308,7 +308,7 @@ The following array of commands within the `pages` property will change location
308308

309309
Other Grunt tasks can run later and validate, compare or otherwise process the page content in different stages of the "Search" scenario.
310310

311-
Navigating to other location, interacting with the page, waiting for some effect to show and saving a snapshot can be divided to different objects in the `pages` array. However, at least One of the `file`, `url` ans `wait` oarameters has to be present in ever object.
311+
Navigating to other location, interacting with the page, waiting for some effect to show and saving a snapshot can be divided to different objects in the `commands` array. However, at least One of the `file`, `url` ans `wait` oarameters has to be present in ever object.
312312

313313
When the commands become too many, you can divide them per page or per other criterion, which corresponds with a scenario and load them from separate modules:
314314

@@ -326,7 +326,7 @@ The module for Addressbook would look like this:
326326
```js
327327
module.exports = {
328328
options: {...},
329-
pages: [
329+
commands: [
330330
{
331331
url: 'https://localhost/addressbook'
332332
wait: '#addressbook.complete'

tasks/html-dom-snapshot.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,47 @@ module.exports = function (grunt) {
3535
force: false
3636
}),
3737
target = this.target,
38-
pages = data.pages || [
38+
pages = data.pages;
39+
if (pages) {
40+
grunt.log.warn('The property "pages" is deprecated. ' +
41+
'Use "commands" with the same content.');
42+
}
43+
const commands = data.commands || pages || [
3944
Object.assign({
4045
file: target
4146
}, data)
4247
],
4348
client = webdriverio.remote({
4449
desiredCapabilities: options.browserCapabilities
4550
});
51+
var urlCount = 0,
52+
fileCount = 0;
4653

47-
client.init()
48-
.then(function () {
49-
return pages.reduce(function (promise, page) {
50-
return promise.then(function () {
51-
return takeSnapshot(page);
52-
});
53-
}, Promise.resolve());
54-
})
55-
.then(function () {
56-
grunt.log.ok(pages.length + ' snapshot(s) created.');
57-
return client.end();
58-
})
59-
.catch(function (error) {
60-
grunt.verbose.error(error.stack);
61-
grunt.log.error(error);
62-
const warn = options.force ? grunt.log.warn : grunt.fail.warn;
63-
warn('Taking snapshot(s) failed.');
64-
})
65-
.then(done);
54+
client.init()
55+
.then(function () {
56+
return commands.reduce(function (promise, command) {
57+
return promise.then(function () {
58+
return performCommand(command);
59+
});
60+
}, Promise.resolve());
61+
})
62+
.then(function () {
63+
grunt.log.ok(commands.length + ' ' +
64+
grunt.util.pluralize(commands.length, 'comand/comands') +
65+
' performed, ' + urlCount + ' ' +
66+
grunt.util.pluralize(urlCount, 'page/pages') +
67+
' visited, ' + fileCount + ' ' +
68+
grunt.util.pluralize(fileCount, 'file/files') +
69+
' written.');
70+
return client.end();
71+
})
72+
.catch(function (error) {
73+
grunt.verbose.error(error.stack);
74+
grunt.log.error(error);
75+
const warn = options.force ? grunt.log.warn : grunt.fail.warn;
76+
warn('Taking snapshots failed.');
77+
})
78+
.then(done);
6679

6780
function ensureDirectory(name) {
6881
return new Promise(function (resolve, reject) {
@@ -76,21 +89,23 @@ module.exports = function (grunt) {
7689
});
7790
}
7891

79-
function takeSnapshot(page) {
80-
const pageOptions = Object.assign({}, options, page.options || {}),
81-
url = page.url,
82-
file = page.file;
83-
var wait = page.wait;
92+
function performCommand(command) {
93+
const commandOptions = Object.assign({}, options, command.options || {}),
94+
url = command.url,
95+
file = command.file;
96+
var wait = command.wait;
8497
if (url) {
8598
grunt.verbose.writeln('Taking a snapshot of ' + url + '...');
99+
++urlCount;
86100
} else {
87101
if (!(file || wait)) {
88102
throw new Error('Missing parameters "url", "file" or "wait" ' +
89103
'in the target "' + target + '".');
90104
}
91105
grunt.verbose.writeln('Preparing the next snapshot...');
92106
}
93-
return client.setViewportSize(pageOptions.viewport)
107+
108+
return client.setViewportSize(commandOptions.viewport)
94109
.then(function () {
95110
return url && client.url(url);
96111
})
@@ -111,9 +126,9 @@ module.exports = function (grunt) {
111126
} else if (typeof wait === 'string') {
112127
if (wait.charAt(0) === '!') {
113128
return client.waitForExist(wait.substr(1).trim(),
114-
pageOptions.selectorTimeout, true);
129+
commandOptions.selectorTimeout, true);
115130
}
116-
return client.waitForExist(wait, pageOptions.selectorTimeout);
131+
return client.waitForExist(wait, commandOptions.selectorTimeout);
117132
} else if (typeof wait === 'number') {
118133
return new Promise(function (resolve) {
119134
setTimeout(resolve, wait);
@@ -124,17 +139,18 @@ module.exports = function (grunt) {
124139
}
125140

126141
function saveContent(html) {
127-
const dest = pageOptions.dest;
142+
const dest = commandOptions.dest;
128143
if (file) {
129144
const target = path.join(dest, file);
130145
grunt.verbose.writeln('Writing the snapshot to ' + target + '...');
131146
return ensureDirectory(dest)
132147
.then(function () {
133148
return new Promise(function (resolve, reject) {
134-
fs.writeFile(target, pageOptions.doctype + html, function (error) {
149+
fs.writeFile(target, commandOptions.doctype + html, function (error) {
135150
if (error) {
136151
reject(error);
137152
} else {
153+
++fileCount;
138154
resolve();
139155
}
140156
});

0 commit comments

Comments
 (0)