From 927208a683c61333838800e91fffe2920ab268ea Mon Sep 17 00:00:00 2001 From: Sergey Tatarintsev Date: Thu, 27 Nov 2014 13:02:50 +0300 Subject: [PATCH] Add `sendFile` action It works better then `sendKeys` for `input[type=file]` elements. Internally, it uses `element.sendKeys` instead of `driver.type(element)` which can upload local file to the server. --- CHANGELOG.md | 2 ++ doc/tests.md | 2 ++ doc/tests.ru.md | 2 ++ lib/browser/actions.js | 25 +++++++++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ae421d1..265070a97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Dev +* Add `sendFile` action which now should be used instead of + `sendKeys` to set a file to `input[type=file]` elements (@SevInf). * Fail only single test if reference image is not found (@SevInf). ## 0.9.3 - 2014-11-07 diff --git a/doc/tests.md b/doc/tests.md index cc5c42e0b..f05fbd935 100644 --- a/doc/tests.md +++ b/doc/tests.md @@ -220,5 +220,7 @@ Full list of special keys (there are shortcuts for commonly used keys): `NUMPAD8`, `NUMPAD9`, `MULTIPLY`, `ADD`, `SEPARATOR`, `SUBTRACT`, `DECIMAL`, `DIVIDE`, `F1`, `F2`, `F3`, `F4`, `F5`, `F6`, `F7`, `F8`, `F9`, `F10`, `F11`, `F12`, `COMMAND` ⇔ `META`, `ZENKAKU_HANKAKU`. +* `sendFile(element, path)` - send file to the specified `input[type=file]` element. `path` must exist at + local system (the one which `gemini` is executed on). * `focus(element)` - set a focus to a specified element. * `setWindowSize(width, height)` - change browser window dimensions. diff --git a/doc/tests.ru.md b/doc/tests.ru.md index 102b72d11..0757aff89 100644 --- a/doc/tests.ru.md +++ b/doc/tests.ru.md @@ -188,5 +188,7 @@ `NUMPAD8`, `NUMPAD9`, `MULTIPLY`, `ADD`, `SEPARATOR`, `SUBTRACT`, `DECIMAL`, `DIVIDE`, `F1`, `F2`, `F3`, `F4`, `F5`, `F6`, `F7`, `F8`, `F9`, `F10`, `F11`, `F12`, `COMMAND` ⇔ `META`, `ZENKAKU_HANKAKU`. +* `sendFile(element, path)` - выбор файла в заданном элементе `input[type=file]`. `path` должен существовать + в локальной системе (там же, где запущен `gemini`). * `focus(element)` - устанавливает фокус на указанный элемент. * `setWindowSize(width, height)` - устанавливает размера окна браузера. diff --git a/lib/browser/actions.js b/lib/browser/actions.js index c68acdcaa..c7ff4670b 100644 --- a/lib/browser/actions.js +++ b/lib/browser/actions.js @@ -1,6 +1,7 @@ 'use strict'; var q = require('q'), + fs = require('q-io/fs'), inherit = require('inherit'), promiseUtil = require('../promise-util'), suiteUtil = require('../suite-util'), @@ -232,6 +233,30 @@ module.exports = inherit({ return this; }, + sendFile: function(element, path) { + var _this = this; + if (typeof path !== 'string') { + throw new TypeError('path must be string'); + } + if (isInvalidElement(element)) { + throw new TypeError('.sendFile() must receive valid element or CSS selector'); + } + + this._pushAction(this.sendFile, function sendFile() { + return fs.isFile(path) + .then(function(isFile) { + if (!isFile) { + return q.reject(new StateError(path + ' should be existing file')); + } + return _this._findElement(element); + }) + .then(function(element) { + return element.sendKeys(path); + }); + }); + return this; + }, + focus: function(element) { if (isInvalidElement(element)) { throw new TypeError('.focus() must receive valid element or CSS selector');