Every year, I have a spreadsheet of ~100 rows I need to submit to a site through a clunky form one row at a time. Instead of wasting my time filling the form by hand 100 times, I wasted my time writing some JavaScript that would take care of it.
The code has served me well for 3 years, but now they've upgraded to an Angular JS app, and now I'm unsure how to fill the forms as if they've been filled by a human.
Take this Angular form for example https://codepen.io/sevilayha/full/xFcdI/
Before I'd be able to set the values of the elements, click on them to ensure a dirty state is triggered, click submit, and be done, but with Angular apps that doesn't seem to work:
function setAndClick(selector, value) {
const el = document.querySelector(selector);
el.value = value;
el.click();
}
setAndClick('[name="name"]', 'Happy Robot');
setAndClick('[name="username"]', 'happyrobot');
setAndClick('[name="email"]', '[email protected]');
Manually click on the fields after they've been filled by a script does trigger a dirty state, but I can't find any event (change, keyup, keydown, etc) that can trigger it programatically.
How can I successfully fill and submit a form in an Angular App with vanilla JavaScript?
$digestcycle where it matches the state of the variables backing theng-modelfor each form element with the state of the input. Programmatically changing the input doesn't trigger the$digest, and thus outside changes aren't reflected, and anything that does trigger the$digestcould potentially wipe out these outside changes. You'll have to hook into the angular app on the page and manually trigger the$digestusing$scope.$apply(), but at that point, you probably should just directly change the variable values, rather than inject the values into the inputs.