diff --git a/lib/helper.js b/lib/helper.js index 72e29aef..6371512f 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -300,13 +300,13 @@ const extendsMixIn = wd => { /** * Type content to input element. - * @name formatInput + * @name formInput * @summary Support: Web(WebView) * @param {string} content of type. * @type utility * @returns {Promise.} */ - wd.addPromiseChainMethod('formatInput', function(string) { + wd.addPromiseChainMethod('formInput', function(string) { const uuid = Date.now(); const list = Array.prototype.slice.call(string); @@ -337,6 +337,45 @@ const extendsMixIn = wd => { }); }); + /** + * Type content to a contentEditable element. + * @name elementInput + * @summary Support: Web(WebView) + * @param {string} content of type. + * @type utility + * @returns {Promise.} + */ + wd.addPromiseChainMethod('elementInput', function(string) { + const uuid = Date.now(); + const list = Array.prototype.slice.call(string); + + if (!list.length) { + return this; + } + + let value = ''; + + return Promise.each(list, (item) => { + value += item; + const script = ` + (function() { + var element = window.__macaca_current_element; + var setValue = Object.getOwnPropertyDescriptor( + window.Element.prototype, + 'innerHTML' + ).set; + setValue.call(element, ${safeQuote(value)}); + var event = document.createEvent('Event'); + event.initEvent('input', true, true); + element.dispatchEvent(event); + })() + `; + return this + .execute(script) + .sleep(10); + }); + }); + /** * Simulate the dom event. * @name domEvent diff --git a/package.json b/package.json index b76d6d57..d959aa21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-wd", - "version": "3.0.2", + "version": "3.1.0", "description": "Macaca wd client", "keywords": [ "macaca",