Skip to content

Instantly share code, notes, and snippets.

@w1ndy
Created December 22, 2015 06:04
Show Gist options
  • Save w1ndy/a9e7320206c6ad74791d to your computer and use it in GitHub Desktop.
Save w1ndy/a9e7320206c6ad74791d to your computer and use it in GitHub Desktop.
Automated survey submitter using PhantomJS
#!/bin/fish
phantomjs --ssl-protocol=any survey.js 100
for i in (seq 0 99)
diff correct.png cap$i.png
end
rm *.png
var system = require('system');
var args = system.args;
var succeed = 0, failed = 0, N = 50;
if (args.length === 1) {
console.log("run with count.");
phantom.exit();
} else {
N = parseInt(args[1]);
}
function runTask(k) {
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log("Task " + k + ": " + msg);
};
page.open('https://www.wenjuan.com/s/QBjuQz/', function(status) {
console.log("Task " + k + " status: " + status);
if(status === "success") {
window.setTimeout(function () {
console.log("Task " + k + ": begin evaluating script...")
var result = page.evaluateAsync(function () {
var questions = $(".question");
if (!questions.length) {
console.log("Error: no question detected");
return false;
}
var select = function (choices, n) {
console.log("choices: ", choices.length);
var p = 0;
for (var i = 0; i < n; i++) {
var t;
do {
t = Math.floor(Math.random() * choices.length);
} while (p & (1 << t));
console.log("select " + t);
$(choices.get(t)).click();
p |= 1 << t;
}
};
questions.each(function (i) {
var e = $(this),
qt = e.attr("questiontype");
console.log("question " + i);
switch (qt) {
case "2":
var choices = $("#" + e.attr("id") + " .icheckbox_div:not(:has(.option_open)) .jqTransformRadio");
select(choices, 1);
break;
case "3":
var choices = $("#" + e.attr("id") + " .icheckbox_div:not(:has(.option_open)) .jqTransformCheckbox");
select(choices, 2);
break;
default:
console.log("Unknown question type: " + qt);
return false;
}
});
$("#next_button").click();
return true;
});
if (result === false) {
failed += 1;
console.log("Task " + k + ": failed.");
return ;
}
window.setTimeout(function () {
console.log("Task " + k + ": completed.");
page.render("cap" + k + ".png");
succeed += 1;
}, 3000);
}, 500);
} else {
failed += 1;
}
});
}
for (var k = 0; k < N; k++) {
console.log("SPAWNING TASK " + k);
window.setTimeout((function (t) {
return function () { runTask(t); };
})(k), 1500 * k);
}
window.setInterval(function () {
if (succeed + failed == N) {
console.log("completed " + (succeed + failed) + " (" + succeed + "," + failed + ")");
phantom.exit();
} else {
console.log("running " + (succeed + failed) + " (" + succeed + "," + failed + ")");
}
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment