Skip to content

Instantly share code, notes, and snippets.

@distracteddev
Created October 29, 2012 00:30
Show Gist options
  • Save distracteddev/3970661 to your computer and use it in GitHub Desktop.
Save distracteddev/3970661 to your computer and use it in GitHub Desktop.
Simulates clicks on web page using jQuery
// @param {string} query # A string in the following format: {element}!{text}
// @param {int} count # Use count to select the Nth element that matches the query
window.click = function click(query, count) {
element = query.split('!')[0];
text = query.split('!')[1];
$(element).each(function(i, el) {
$el = $(el);
var condition = $el.text() === text;
if (count) {
condition = ($el.text() === text && count === i);
}
if (condition) {
$el[0].click();
}
});
};
setTimeout(function simulate() {
var delay = 300;
var step = 1;
function simulateClick(query, count) {
var args = arguments;
setTimeout(function() {
currentStep = step;
window.click.apply(window, args);
}, delay*step);
step++;
}
// SAMPLE CODE THAT SIMULATES GOING TO STEP 3 OF THE
// 6th APPLICATION AVAILABLE
simulateClick('a!Apps');
// click the 6th button with the text 'Deploy Now'
simulateClick('button!Deploy Now', 6);
simulateClick('button!Next');
simulateClick('button!Next');
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment