I'm trying to test the visual state of a number of buttons -- hover, click, focus -- and was looking for a way to not copy basically the same casper.then()
command over and over. I thought I should be able to do this in a simple loop.
I made a small array of the (currently) 5 buttons and while-looped through them adding CasperJS steps for each of the states I'd like to record. Unfortunately, only the last of the steps is actually performed.
I've read a number of posts about looping, but they all seem to involve clicking links on a page or some other scenario that doesn't seem to match what I'm looking for.
Maybe I'm just being dense? Code below...
casper.thenOpen( 'docs/buttons/test.html' );
var buttonStyles = [
{ selector: '.grey0', title: 'Button - Grey 0' },
{ selector: '.grey25', title: 'Button - Grey 25' },
{ selector: '.grey50', title: 'Button - Grey 50' },
{ selector: '.grey75', title: 'Button - Grey 75' },
{ selector: '.grey100', title: 'Button - Grey 100' },
];
while (buttonStyles.length > 0) {
buttonStyle = buttonStyles.pop();
casper.then(function(){
phantomcss.screenshot(buttonStyle.selector, buttonStyle.title);
});
casper.then(function(){
this.mouse.move(buttonStyle.selector);
phantomcss.screenshot(buttonStyle.selector, buttonStyle.title + ' - Hover');
});
casper.then(function(){
this.mouse.down(buttonStyle.selector);
phantomcss.screenshot(buttonStyle.selector, buttonStyle.title + ' - Down');
});
}
casper.test.done();