I'd like to try to randomize the order of filling out form fields.
async function processForm() {
....
async function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms*(Math.random() +.5)));
}
async function func1() {
await page.type(...);
}
async function func2() {
await page.type(...);
}
async function func3() {
await page.click(...);
}
async function func4() {
await page.click(...);
}
var a = function () { func1(); },
b = function () { func2(); },
c = function () { func3(); },
d = function () { func4(); },
array = [a, b, c, d];
array = array.map(function (a, i, o) {
var j = (Math.random() * (o.length - i) | 0) + i,
t = o[j];
o[j] = a;
return t;
});
for ( const a of array) {
await timeout(4000);
await a();
}
await timeout(4000);
await page.click("button[type='submit']");
....
}
I tried sticking console.log statements in each function and after the submit. I find that it indeed is random, but sometimes the submit occurs before the 4 func.
I used this example (JavaScript random order functions) to randomize but I am struggling to have them be called sequentially.
I thought that with async functions and await, it should wait for completion before proceeding, therefore the submit should always occur AFTER the for loop however this does not seem to be happening.