I created the following checkGrid function which takes a count and then another two parameters. It's a solution which hardcodes in the selectOption calls and is not very flexible:
checkGrid = function(expectCount, typeParam, selectParam) {
it('Check', function () {
selectOption('examTypeSelect', typeParam).click();
selectOption('examStatusSelect', selectParam).click();
});
}
checkGrid(10, '*', '*');
Now I would like to make this more flexible so that it accepts arguments for any number of selectOption functions. So I was thinking of something that I could call like this. HEre what I would need is for the checkGrid function to call the selectOption three times:
checkGrid(10, [{'examTypeSelect','*'}
{'examStatusSelect', '*'},
{'abcSelect','29'}]);
How could I take an array object that's the second parameter for checkGrid and make it so that it calls any number of
selectOption functions depending on how many elements there are in the array?
1 Answer 1
By using a loop over the array and .apply to call each function with a variable number of arguments.
checkGrid = function(expectCount, stuff) {
it('Check', function () {
for (var i = 0; i < stuff.length; ++i) {
selectOption.apply(this, stuff[i]).click();
}
});
}
You would call this not with your proposed syntax (which is invalid), but like
checkGrid(10, [
['examTypeSelect','*'],
['examStatusSelect', '*'],
['abcSelect','29','and','perhaps','more','parameters']
]
);
Finally, it's unclear what expectCount is supposed to do but I left it there because your original code presumably does something with it.
6 Comments
this inside the body of selectOption.this would actually be the default since the original code doesn't pass the enclosed this.{'examTypeSelect','*'} etc is incorrect syntax, period..apply() here to do what you want, as long as you know each call will have 2 arguments. selectOption(stuff[0], stuff[1]);. Also, you don't need an Array of Arrays (or Objects). You can pass as many objects as you want to checkGrid, and then loop the arguments object starting at index 1. This way you're using checkGrid as a variadic function, meaning the number of arguments it receives is determined when invoked.
forloop.