Skip to content

Commit c776175

Browse files
committed
feat: Add the "keys" instruction
Allow sending key strokes to the browser.
1 parent 302d152 commit c776175

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

Gruntfile.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ module.exports = function (grunt) {
225225
}
226226
});
227227
}
228+
},
229+
{
230+
keys: 'test',
231+
wait: function (browser) {
232+
return browser.getValue('input')
233+
.then(function (value) {
234+
if (value !== 'test') {
235+
throw new Error ('sending text failed');
236+
}
237+
});
238+
}
239+
},
240+
{
241+
keys: ['Home', 'Delete'],
242+
wait: function (browser) {
243+
return browser.getValue('input')
244+
.then(function (value) {
245+
if (value !== 'est') {
246+
throw new Error ('sending key strokes failed', value);
247+
}
248+
});
249+
}
228250
}
229251
],
230252
scenarios: 'test/scenarios/*.js'

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,18 @@ You can use sub-tasks, `commands` and `scenarios` to structure your code and exe
217217

218218
### Instructions
219219

220-
One of the instructions has to be present in every command. These properties are evaluated (and their effect is executed) in the order, which they are listed below.
220+
One of the instructions has to be present in every command. These properties are evaluated (and their effect is executed) in the order, in which they are listed below:
221+
222+
* url
223+
* go
224+
* clearValue
225+
* setValue
226+
* addValue
227+
* moveCursor
228+
* click
229+
* keys
230+
* wait
231+
* file
221232

222233
#### url
223234
Type: `String`
@@ -337,6 +348,20 @@ Triggers a click event on an element with the specified selector.
337348
}
338349
```
339350

351+
#### keys
352+
Type: `String|Array`
353+
354+
Sends either a text (string) typed by keys, or single keystrokes (array) to the browser.
355+
356+
```js
357+
{
358+
url: 'https://google.com',
359+
click: 'input[name=btnK]',
360+
keys: 'test',
361+
file: 'google'
362+
}
363+
```
364+
340365
#### wait
341366
Type: `Number` | `String` | `Function` | `Array` (optional)
342367

@@ -639,6 +664,7 @@ your code using Grunt.
639664
640665
## Release History
641666
667+
* 2018-02-22 [v0.4.0] Allow sending key strokes to the browser
642668
* 2018-01-30 [v0.3.0] Allow specifying test commands in separate modules
643669
* 2018-01-27 [v0.2.0] Allow saving screenshots in addition to snapshots
644670
* 2017-11-18 [v0.1.0] Allow separate navigation, page interaction and saving snapshots

tasks/html-dom-snapshot.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module.exports = function (grunt) {
146146
clearValue = command.clearValue,
147147
setValue = command.setValue,
148148
addValue = command.addValue,
149+
keys = command.keys,
149150
click = command.click,
150151
viewport = commandOptions.viewport,
151152
screenshots = commandOptions.screenshots;
@@ -230,6 +231,15 @@ module.exports = function (grunt) {
230231
return client.click(click);
231232
}
232233
})
234+
.then(function () {
235+
if (keys) {
236+
const message = Array.isArray(keys) ?
237+
'Send keys "' + keys.join('", "') + '".' :
238+
'Send test "' + keys + '".';
239+
grunt.verbose.writeln(message);
240+
return client.keys(keys);
241+
}
242+
})
233243
.then(waitForContent)
234244
.then(function () {
235245
if (snapshots && screenshots) {

0 commit comments

Comments
 (0)