diff --git a/node_modules/marionette-helper/index.js b/node_modules/marionette-helper/index.js index 50a3e8c2..7135457f 100644 --- a/node_modules/marionette-helper/index.js +++ b/node_modules/marionette-helper/index.js @@ -181,10 +181,81 @@ MarionetteHelper.prototype = { }); }, + /** + * Tap input element and fill the value. + * To do: Support the time, date, datetime, and datetime-local input + * correctly. + * Please refer to http://bugzil.la/976453. + * + * @param {Marionette.Element|String} el marionette element object or + * css selector. + * @param {String|Object} input text or datetime object for the input. + */ + fillInputField: function(el, input) { + if (!isElement(el)) { + el = this.client.findElement(el); + } + var inputText; + if (input instanceof Date) { + // XXX: bug 978685 + // We cannot get type through element.getAttribute('type') if the type is + // datetime or datetime-local, so we use below workaround. + var eleType = this.client.executeScript(function(el) { + return el.getAttribute('type'); + }, [el]); + + switch (eleType) { + case 'date': + inputText = input.getFullYear() + '-' + + addLeadZero(input.getMonth()) + + '-' + addLeadZero(input.getDate()); + break; + case 'time': + inputText = addLeadZero(input.getHours()) + ':' + + addLeadZero(input.getMinutes()); + break; + case 'datetime': + inputText = input.toISOString(); + break; + case 'datetime-local': + inputText = input.getFullYear() + '-' + + addLeadZero(input.getMonth()) + + '-' + addLeadZero(input.getDate()) + 'T' + + addLeadZero(input.getHours()) + ':' + + addLeadZero(input.getMinutes()) + ':' + + addLeadZero(input.getSeconds()) + '.' + + input.getMilliseconds(); + break; + default: + throw new Error('The ' + eleType + ' type element is not supported' + + ' when the input param is a Date object.'); + } + } else { + inputText = input; + } + + // Below script is to trigger input event on the input correctly and fill + // the data. + this.client.executeScript(function(el, value) { + el.value = value; + var evt = new Event('input', { + 'view': window, + 'bubbles': true, + 'cancelable': true + }); + el.dispatchEvent(evt); + }, [el, inputText]); + + // We add lead zero on single digit. ex: 1 -> 01, 9 -> 09. + function addLeadZero(num) { + return num >= 10 ? num : ('0' + num); + } + }, + /** * select specific option from target select element * @param {Marionette.Element|String} el element or some css selector. - * @param {String} optionText text for the option + * @param {String} optionText text for the option. */ tapSelectOption: function(el, optionText) { var selectedOption = null; diff --git a/node_modules/marionette-helper/package.json b/node_modules/marionette-helper/package.json index 07d85966..76295a99 100644 --- a/node_modules/marionette-helper/package.json +++ b/node_modules/marionette-helper/package.json @@ -1,6 +1,6 @@ { "name": "marionette-helper", - "version": "0.2.1", + "version": "0.2.2", "author": { "name": "Gareth Aye", "email": "gaye@mozilla.com" diff --git a/node_modules/marionette-helper/test/integration/fakeapp/index.html b/node_modules/marionette-helper/test/integration/fakeapp/index.html index e18cd3e4..7035052c 100644 --- a/node_modules/marionette-helper/test/integration/fakeapp/index.html +++ b/node_modules/marionette-helper/test/integration/fakeapp/index.html @@ -17,5 +17,11 @@ + + + + + + diff --git a/node_modules/marionette-helper/test/integration/index_test.js b/node_modules/marionette-helper/test/integration/index_test.js new file mode 100644 index 00000000..8b2680f0 --- /dev/null +++ b/node_modules/marionette-helper/test/integration/index_test.js @@ -0,0 +1,83 @@ +suite('MarionetteHelper.fillInputFieldAndtapSelectOption', function() { + + // Require needed files. + var FakeApp = require('./lib/fake_app'); + marionette.plugin('helper', require('../../index')); + marionette.plugin('apps', require('marionette-apps')); + + var helper; + var fakeApp; + var FAKE_APP_ORIGIN = 'fakeapp.gaiamobile.org'; + + var apps = {}; + apps[FAKE_APP_ORIGIN] = __dirname + '/fakeapp'; + + var testTime; + var client = marionette.client({ + settings: { + 'ftu.manifestURL': null, + 'lockscreen.enabled': false + }, + apps: apps + }); + + // We add lead zero on single digit. ex: 1 -> 01, 9 -> 09. + function addLeadZero(num) { + return num >= 10 ? num : ('0' + num); + } + + setup(function(done) { + helper = client.helper; + fakeApp = new FakeApp(client, 'app://' + FAKE_APP_ORIGIN); + fakeApp.launch(); + testTime = new Date(); + setTimeout(done, 2500); // Instead of using the BootWatcher. + }); + + test('should set on option', function() { + var optionValue = 'option2'; + helper.tapSelectOption('#select', optionValue); + assert.ok(fakeApp.isSpecificSelectOptionSelected(optionValue)); + }); + + test('should set value on input', function() { + var inputValue = 'inputtest'; + helper.fillInputField('#input', inputValue); + assert.equal(fakeApp.inputElementValue, inputValue); + }); + + test('should set date on input', function() { + var inputValue = + testTime.getFullYear() + '-' + addLeadZero(testTime.getMonth()) + '-' + + addLeadZero(testTime.getDate()); + helper.fillInputField('#input-date', testTime); + assert.equal(fakeApp.inputDateElementValue, inputValue); + }); + + test('should set time on input', function() { + var inputValue = addLeadZero(testTime.getHours()) + ':' + + addLeadZero(testTime.getMinutes()); + helper.fillInputField('#input-time', testTime); + assert.equal(fakeApp.inputTimeElementValue, inputValue); + }); + + test('should set datetime on input', function() { + var inputValue = testTime.toISOString(); + + helper.fillInputField('#input-datetime', testTime); + assert.equal(fakeApp.inputDatetimeElementValue, inputValue); + }); + + test('should set datetime-local on input', function() { + var inputValue = testTime.getFullYear() + '-' + + addLeadZero(testTime.getMonth()) + '-' + + addLeadZero(testTime.getDate()) + 'T' + + addLeadZero(testTime.getHours()) + ':' + + addLeadZero(testTime.getMinutes()) + ':' + + addLeadZero(testTime.getSeconds()) + '.' + + testTime.getMilliseconds(); + + helper.fillInputField('#input-datetime-local', testTime); + assert.equal(fakeApp.inputDatetimeLocalElementValue, inputValue); + }); +}); diff --git a/node_modules/marionette-helper/test/integration/lib/fake_app.js b/node_modules/marionette-helper/test/integration/lib/fake_app.js index 4d2ded44..050edd6c 100644 --- a/node_modules/marionette-helper/test/integration/lib/fake_app.js +++ b/node_modules/marionette-helper/test/integration/lib/fake_app.js @@ -9,11 +9,37 @@ function FakeApp(client, origin) { FakeApp.Selector = Object.freeze({ selectElement: '#select', - selectedOptionElement: '#select option:checked' + selectedOptionElement: '#select option:checked', + inputElement: '#input', + inputDateElement: '#input-date', + inputTimeElement: '#input-time', + inputDatetimeElement: '#input-datetime', + inputDatetimeLocalElement: '#input-datetime-local' }); FakeApp.prototype = { client: null, + + get inputElementValue() { + return this.client.findElement( + FakeApp.Selector['inputElement']).getAttribute('value'); + }, + get inputDateElementValue() { + return this.client.findElement( + FakeApp.Selector['inputDateElement']).getAttribute('value'); + }, + get inputTimeElementValue() { + return this.client.findElement( + FakeApp.Selector['inputTimeElement']).getAttribute('value'); + }, + get inputDatetimeElementValue() { + return this.client.findElement( + FakeApp.Selector['inputDatetimeElement']).getAttribute('value'); + }, + get inputDatetimeLocalElementValue() { + return this.client.findElement( + FakeApp.Selector['inputDatetimeLocalElement']).getAttribute('value'); + }, get selectElement() { return this.client.findElement(FakeApp.Selector.selectElement); }, diff --git a/node_modules/marionette-helper/test/integration/tap_select_option_test.js b/node_modules/marionette-helper/test/integration/tap_select_option_test.js deleted file mode 100644 index b71f4aa2..00000000 --- a/node_modules/marionette-helper/test/integration/tap_select_option_test.js +++ /dev/null @@ -1,35 +0,0 @@ -suite('MarionetteHelper.tapSelectOption', function() { - - // Require needed files - var FakeApp = require('./lib/fake_app'); - marionette.plugin('helper', require('../../index')); - marionette.plugin('apps', require('marionette-apps')); - - var helper; - var fakeApp; - var FAKE_APP_ORIGIN = 'fakeapp.gaiamobile.org'; - - var apps = {}; - apps[FAKE_APP_ORIGIN] = __dirname + '/fakeapp'; - - var client = marionette.client({ - settings: { - 'ftu.manifestURL': null, - 'lockscreen.enabled': false - }, - apps: apps - }); - - setup(function(done) { - helper = client.helper; - fakeApp = new FakeApp(client, 'app://' + FAKE_APP_ORIGIN); - fakeApp.launch(); - setTimeout(done, 2500); // Instead of using the BootWatcher. - }); - - test('should set on option', function() { - var optionValue = 'option2'; - helper.tapSelectOption('#select', optionValue); - assert.ok(fakeApp.isSpecificSelectOptionSelected(optionValue)); - }); -});