Skip to content

Commit 2df4191

Browse files
committed
feat: Add a new instruction focus to focus an element
1 parent e313678 commit 2df4191

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

Gruntfile.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,24 @@ module.exports = function (grunt) {
319319
value: '<div class="class" tabindex="0">Text</div>'
320320
}
321321
},
322+
coverage ? { wait: 1 } : {
323+
focus: 'body',
324+
wait: function (browser) {
325+
return browser.hasFocus('body')
326+
.then(function (value) {
327+
if (value !== false) {
328+
throw new Error('focus on body failed')
329+
}
330+
})
331+
}
332+
},
322333
{
323-
clickIfVisible: 'select',
334+
clickIfVisible: 'input',
324335
wait: function (browser) {
325-
return browser.hasFocus('select')
336+
return browser.hasFocus('input')
326337
.then(function (value) {
327-
if (value === false) {
328-
throw new Error('clickIfVisible on select failed')
338+
if (value !== false) {
339+
throw new Error('clickIfVisible on invisible input failed')
329340
}
330341
})
331342
}

tasks/html-dom-snapshot.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const mkdirp = require('mkdirp')
88
// order in a command object. In this order they will be looked for and
99
// executed.
1010
const instructionKeys = [
11-
'setViewport', 'url', 'go', 'scroll', 'clearValue', 'setValue', 'addValue',
12-
'selectOptionByIndex', 'selectOptionByValue', 'moveCursor',
13-
'click', 'clickIfVisible', 'keys', 'wait', 'hasAttribute', 'hasClass', 'hasValue',
14-
'hasText', 'hasInnerHtml', 'hasOuterHtml',
11+
'setViewport', 'url', 'go', 'scroll', 'focus', 'clearValue', 'setValue',
12+
'addValue', 'selectOptionByIndex', 'selectOptionByValue', 'moveCursor',
13+
'click', 'clickIfVisible', 'keys', 'wait', 'hasAttribute', 'hasClass',
14+
'hasValue', 'hasText', 'hasInnerHtml', 'hasOuterHtml',
1515
'isEnabled', 'isExisting', 'isFocused', 'isSelected', 'isVisible',
1616
'isVisibleWithinViewport', 'isNotEnabled', 'isNotExisting',
1717
'isNotFocused', 'isNotSelected', 'isNotVisible',

tasks/instructions/focus.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
// Istanbul instruments the callback to be executed in the browser,
4+
// introduces variables there, which are defined only in the node
5+
// part of the code and thus breaks the code.
6+
7+
/* istanbul ignore next */
8+
9+
module.exports = {
10+
detect: function (command) {
11+
return !!command.focus
12+
},
13+
14+
perform: function (grunt, target, client, command) {
15+
const focus = command.focus
16+
grunt.verbose.writeln('Focus "' + focus + '".')
17+
return client.execute(function (selector) {
18+
var element = document.querySelector(selector)
19+
if (!element) {
20+
return false
21+
}
22+
element.focus()
23+
}, focus)
24+
.then(function (value) {
25+
if (value === false) {
26+
throw new Error('"' + focus + '" does not exist.')
27+
}
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)