Skip to content

Commit

Permalink
feat: support elementInput (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudafeng committed Mar 14, 2019
1 parent d1daf9b commit 71df74e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 41 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<boolean>}
*/
wd.addPromiseChainMethod('formatInput', function(string) {
wd.addPromiseChainMethod('formInput', function(string) {
const uuid = Date.now();
const list = Array.prototype.slice.call(string);

Expand Down Expand Up @@ -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.<boolean>}
*/
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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-wd",
"version": "3.0.2",
"version": "3.1.0",
"description": "Macaca wd client",
"keywords": [
"macaca",
Expand Down

0 comments on commit 71df74e

Please sign in to comment.