Skip to content

Commit

Permalink
write api to wait for element to be focusable, #714
Browse files Browse the repository at this point in the history
  • Loading branch information
nehashri authored and negiDharmendra committed Jul 29, 2019
1 parent ca98d22 commit ea36a9b
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions lib/taiko.js
Expand Up @@ -1038,19 +1038,34 @@ module.exports.write = async (text, into, options = { delay: 10 }) => {
if (into) {
const selector = isString(into) ? textField(into) : into;
const elems = await handleRelativeSearch(await elements(selector), []);
for (let elem of elems) {
await _focus(elem);
const activeElement = await runtimeHandler.activeElement();
if (!activeElement.notWritable) {
await _write(text, activeElement, options);
text = (activeElement.isPassword || options.hideText) ? '*****' : text;
desc = `Wrote ${text} into the ` + description(selector, true);
return;
await waitUntil(async () => {
for (let elem of elems) {
try{
await _focus(elem);
let activeElement = await runtimeHandler.activeElement();
if (activeElement.notWritable) continue;
return true;
} catch (e) {
continue;
}
}
}
throw new Error('Element focused is not writable');
return false;
}, 100, 1000).catch(() => {
throw new Error('Element focused is not writable');
});
let activeElement = await runtimeHandler.activeElement();
await _write(text, activeElement, options);
text = (activeElement.isPassword) ? '*****' : text;
desc = `Wrote ${text} into the ` + description(selector, true);
return;
} else {
const activeElement = await runtimeHandler.activeElement();
let activeElement = await runtimeHandler.activeElement();
if (activeElement.notWritable) {
await waitUntil(async () => !(await runtimeHandler.activeElement()).notWritable, 100, 10000).catch(() => {
throw new Error('Element focused is not writable');
});
activeElement = await runtimeHandler.activeElement();
}
await _write(text, activeElement, options);
text = (activeElement.isPassword || options.hideText) ? '*****' : text;
desc = `Wrote ${text} into the focused element.`;
Expand All @@ -1061,12 +1076,6 @@ module.exports.write = async (text, into, options = { delay: 10 }) => {
};

const _write = async (text, activeElement, options) => {
if (activeElement.notWritable) {
await waitUntil(async () => !(await runtimeHandler.activeElement()).notWritable, 100, 10000).catch(() => {
throw new Error('Element focused is not writable');
});
activeElement = await runtimeHandler.activeElement();
}
if (defaultConfig.headful) await highlightElemOnAction(activeElement.nodeId);
for (const char of text) {
await input.dispatchKeyEvent({ type: 'keyDown', text: char });
Expand Down

0 comments on commit ea36a9b

Please sign in to comment.