Created
December 21, 2012 08:01
-
-
Save roycollings/4351350 to your computer and use it in GitHub Desktop.
AngularJS E2E: loop through a list and test each item in a separate "it" statement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PROBLEM: | |
// ======== | |
// | |
// If you have a list of 'something', perhaps links to another page, and you want to click each item, | |
// then run some tests (and possibly click more links in the next page and run tests etc...). | |
// You could have dozens (or even hundreds) of tests per list item. | |
// In AngularJS E2E you need to enclose the whole thing in ONE "it" statement | |
// (because otherwise you cannot see the 'element' object), something like this: | |
it ('clicking each link takes you to the correct page', function() { | |
element('li a').query(function (selectedElements, done) { | |
selectedElements.each(function(idx,elm) { | |
//... do LOTS of tests on this list item ... | |
}); | |
}); | |
}); | |
// Every test in there against every list item is now in the same "it", so finding the source of a problem | |
// could be a serious drag. Also, if one test fails then no others in here will run | |
// (because the application thinks they are all part of one big test). | |
// | |
// So the challenge is to process each list item in it's own "it" statement. | |
// I couldn't find a way to do it using pure AngularJS E2E, I had to mix it up a bit with some | |
// jQuery and javascript. But anyway, here it is (in theory - need time to test it!). | |
// | |
// SOLUTION: | |
// ========= | |
// | |
// Wait for the list elements to resolve, then | |
// assign the count to the variable "x". | |
// | |
var x = []; | |
it('get list count into "x"', function() { | |
var y = element('li a').count(); | |
var z = setInterval(function() { | |
if (y.fulfilled) { | |
clearInterval(z); | |
console.log("pushing to x: " + y.value); | |
x.push(y.value); | |
} | |
},200); | |
}); | |
// | |
// Now we wait until the variable is set, then we can | |
// loop through each list item, clicking + | |
// running tests etc... against each one, in individual | |
// "it" statements. | |
// | |
var z2 = setInterval(function() { | |
if (x.length > 0) { | |
// x contains something, so now we can proceed. | |
clearInterval(z2); | |
$.each(x, function(idx, x_item) { | |
it ('"' + x_item + '" is displayed in the list', function() { | |
element('li a:eq(' + x_item + ')').click(); | |
// ... do some tests then use "browser().navigateTo" to come back here ready | |
// for the next test ... | |
}); | |
}); | |
} | |
}, 200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment