[api] Introduce Page.select method (#779)

This patch adds `Page.select` method to select
values in a `select` tag.
This commit is contained in:
Alix Axel
2017-09-25 11:23:34 +02:00
committed by Andrey Lushnikov
parent acdb588964
commit 45f264024b
4 changed files with 136 additions and 0 deletions

View File

@@ -693,6 +693,29 @@ class Page extends EventEmitter {
await handle.dispose();
}
/**
* @param {string} selector
* @param {!Array<string>} values
*/
async select(selector, ...values) {
await this.$eval(selector, (element, values) => {
if (element.nodeName.toLowerCase() !== 'select')
throw new Error('Element is not a <select> element.');
const options = Array.from(element.options);
if (element.multiple) {
for (const option of options)
option.selected = values.includes(option.value);
} else {
element.value = values.shift();
}
element.dispatchEvent(new Event('change'));
element.dispatchEvent(new Event('input'));
}, values);
}
/**
* @param {string} text
* @param {{delay: (number|undefined)}=} options