Skip to content

Instantly share code, notes, and snippets.

@stevelacey
Last active May 10, 2024 15:02
Show Gist options
  • Select an option

  • Save stevelacey/6776357 to your computer and use it in GitHub Desktop.

Select an option

Save stevelacey/6776357 to your computer and use it in GitHub Desktop.
JS automation of survey completion (with weighting)
function blam(sentiment) {
var sentiment = sentiment || null;
$('input:text, textarea').each(function() {
$(this).val('Generic ' + sentiment + '\'ve input!');
});
$('.question:contains("Email") input:text').val('[email protected]');
$('.date-picker').val('October 08, 2014');
$('select').each(function() {
$options = $(this).find('option:not(:empty)');
$options.eq(~~(Math.random() * $options.length)).prop('selected', true);
});
$('.question:contains("Ranking")').each(function() {
var ranked = [];
$(this).find('select').each(function() {
$options = $(this).find('option:not(:empty)').not(
ranked
.map(function(option) {
return '[value="' + option + '"]';
})
.join(',')
);
$options.eq(~~(Math.random() * $options.length)).prop('selected', true);
ranked.push($(this).val());
});
});
questions = $
.unique(
$('input:radio').map(function() {
return $(this).attr('name');
})
)
.map(function(i, name) {
return $('input[name="' + name + '"]');
})
;
$.each(questions, function(i, $question) {
var multiplier = Math.random();
if (sentiment !== null) {
// weight 75% in the direction of the sentiment
if (sentiment == '+' && Math.random() < .75 || sentiment == '-' && Math.random() > .75) {
// +'ve weighting
multiplier = multiplier * .5 + .5
} else {
// -'ve weighting
multiplier = multiplier * .5
}
}
var x = Math.round($question.length * multiplier);
$(this).eq(x).click();
});
}
blam('+');
$('form').submit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment