Skip to content

Instantly share code, notes, and snippets.

@sbolel
Created March 27, 2022 15:56
Show Gist options
  • Select an option

  • Save sbolel/59561060b1fe3dcf45d52b1bd39493e9 to your computer and use it in GitHub Desktop.

Select an option

Save sbolel/59561060b1fe3dcf45d52b1bd39493e9 to your computer and use it in GitHub Desktop.
Autofill forms for testing
const setNativeValue = (element, value) => {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) prototypeValueSetter.call(element, value);
else valueSetter.call(element, value);
element.dispatchEvent(new Event('input', {bubbles: true}));
};
const setValue = (name, value) => setNativeValue(document.querySelector(`input[name="${name}"]`), `${value}`)
const id = () => {
const d = new Date();
return `${d.getMonth()}${d.getDay()}.${d.getHours()}${d.getMinutes()}${d.getSeconds()}`;
}
const example = () => {
[
['url', `${id}.website.com`]
['email', `${id}@email.com`]
['password', 'TestThis!l33t']
].map(i => setValue.apply(null, i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment