1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/drivers/cdp/templates/select.go
Tim Voronov 8e13cf9134
Refactored input and select (#331)
* Refactored input and select

* WIP

* Fixed serialization

* Fixed scriolling

* Fixed XPath result handling

* Renamed some methods
2019-07-16 18:17:42 -04:00

57 lines
1.0 KiB
Go

package templates
import (
"fmt"
"github.com/MontFerret/ferret/pkg/drivers"
)
func selectBase(values string) string {
return fmt.Sprintf(`
const values = %s;
if (el.nodeName.toLowerCase() !== 'select') {
throw new Error('element is not a <select> element.');
}
const options = Array.from(el.options);
el.value = undefined;
for (var option of options) {
option.selected = values.includes(option.value);
if (option.selected && !el.multiple) {
break;
}
}
el.dispatchEvent(new Event('input', { 'bubbles': true }));
el.dispatchEvent(new Event('change', { 'bubbles': true }));
return options.filter(option => option.selected).map(option => option.value);
`, values,
)
}
func Select(values string) string {
return fmt.Sprintf(`
(el) => {
%s
}
`, selectBase(values),
)
}
func SelectBySelector(selector, values string) string {
return fmt.Sprintf(`
const el = document.querySelector('%s');
if (el == null) {
throw new Error("%s")
}
%s
`, selector, drivers.ErrNotFound, selectBase(values),
)
}