diff --git a/Gruntfile.js b/Gruntfile.js index 20da493..3403b83 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -188,6 +188,7 @@ module.exports = function (grunt) { }, { url: 'http://localhost:8881/test/pages/input.html', + scroll: 'input', moveCursor: 'input', click: 'input', wait: function (browser) { diff --git a/INSTRUCTIONS.md b/INSTRUCTIONS.md index 5f7175c..11f6fea 100644 --- a/INSTRUCTIONS.md +++ b/INSTRUCTIONS.md @@ -5,6 +5,7 @@ One of the instructions has to be present in every command. These properties are - [setViewport](#setviewport) - [url](#url) - [go](#go) +- [scroll](#scroll) - [clearValue](#clearvalue) - [setValue](#setvalue) - [addValue](#addvalue) @@ -76,6 +77,26 @@ Navigates backwards or forwards using the browser history, or refreshes the curr } ``` +## scroll +Type: `String` | `Object` + +Scrolls the page, so that the element with the specified selector or mouse coordinates become visible. If an object is used for the specification, it should contain the following properties: + +* `selector` - `String` - selector of the element to move the mose to. +* `offset` - `Object` - relative offset to the top left corner of the specified element expected as `left` and `top` numeric properties. + +```js +{ + url: 'https://google.com', + scroll: { + selector: '#lst-ib' + }, + file: 'google' +} +``` + +The `offset` value can be specified instead of the `selector` and shares the format with the [`moveCursor`](#moveCursor) instruction. + ## clearValue Type: `String` diff --git a/README.md b/README.md index 7a239e0..7087d44 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,7 @@ One of the [instructions] has to be present in every command. These properties a * [setViewport](INSTRUCTIONS.md#setviewport) * [url](INSTRUCTIONS.md#url) * [go](INSTRUCTIONS.md#go) +* [scroll](INSTRUCTIONS.md#scroll) * [clearValue](INSTRUCTIONS.md#clearvalue) * [setValue](INSTRUCTIONS.md#setvalue) * [addValue](INSTRUCTIONS.md#addvalue) @@ -508,7 +509,7 @@ your code using Grunt. ## Release History - * 2018-05-14 [v1.3.0] Allow saving snapshots to sub-directories, file numbering per-directory + * 2018-05-14 [v1.3.0] Allow saving snapshots to sub-directories, file numbering per-directory, add `scroll` instruction * 2018-05-11 [v1.2.0] Introduce delay after every instruction to be able to visually follow the actions when debugging * 2018-03-29 [v1.1.0] Allow specifying all initialization parameters supported by WebdriverIO * 2018-03-28 [v1.0.2] Stop Selenium and Chromedriver processes on unexpected Grunt process abortion diff --git a/tasks/html-dom-snapshot.js b/tasks/html-dom-snapshot.js index 6bcd153..7c79745 100644 --- a/tasks/html-dom-snapshot.js +++ b/tasks/html-dom-snapshot.js @@ -15,7 +15,7 @@ const {basename, dirname, isAbsolute, join} = require('path') const mkdirp = require('mkdirp') const nodeCleanup = require('node-cleanup') const instructions = [ - 'setViewport', 'url', 'go', 'clearValue', 'setValue', 'addValue', + 'setViewport', 'url', 'go', 'scroll', 'clearValue', 'setValue', 'addValue', 'selectOptionByIndex', 'selectOptionByValue', 'moveCursor', 'click', 'keys', 'wait', 'hasAttribute', 'hasClass', 'hasValue', 'hasText', 'hasInnerHtml', 'hasOuterHtml', diff --git a/tasks/instructions/scroll.js b/tasks/instructions/scroll.js new file mode 100644 index 0000000..ddb623d --- /dev/null +++ b/tasks/instructions/scroll.js @@ -0,0 +1,22 @@ +'use strict' + +module.exports = { + detect: function (command) { + return !!command.scroll + }, + + perform: function (grunt, target, client, command) { + let scroll = command.scroll + if (typeof scroll === 'string') { + scroll = {selector: scroll} + } + const selector = scroll.selector + if (selector) { + grunt.verbose.writeln('Move cursor to "' + selector + '".') + return client.scroll(selector) + } + const offset = scroll.offset || {} + grunt.verbose.writeln('Move cursor to ' + JSON.stringify(offset) + '.') + return client.scroll(offset.left, offset.top) + } +}