Is there a way to implement loops so that the following code can be reduced?
var navItems = element.all(by.repeater('item in mc.navItems'));
expect(navItems.count()).toEqual(4);
expect(navItems.get(0).getText()).toEqual('page1'.toUpperCase());
navItems.get(0).click();
expect(browser.getLocationAbsUrl()).toEqual('/page1');
expect(navItems.get(1).getText()).toEqual('page2'.toUpperCase());
navItems.get(1).click();
expect(browser.getLocationAbsUrl()).toEqual('/page2');
expect(navItems.get(2).getText()).toEqual('page3'.toUpperCase());
navItems.get(2).click();
expect(browser.getLocationAbsUrl()).toEqual('/page3');
expect(navItems.get(3).getText()).toEqual('page4'.toUpperCase());
navItems.get(3).click();
expect(browser.getLocationAbsUrl()).toEqual('/page4');
I've tried multiple ways but they all fail because I'm not sure how to get them to wait until the promised value is returned.
Here's one way that fails:
var navItems = element.all(by.repeater('item in mc.navItems'));
for(var i = 0; i < navItems.count(); i++) {
expect(navItems.get(i).getText()).toEqual(expectedValue[i].toUpperCase());
navItems.get(i).click();
expect(browser.getLocationAbsUrl()).toEqual('/' + expectedValue[i]);
}
where expectedValue = ['page1', 'page2', 'page3', 'page4'];
The problem with this is that the for loop evaluates the condition even before the promised value is returned and so the contents of the loop are never executed.
I'm sure I can use a then
and attach a callback function to count()
but that would only make things messy because I'd have to make navItems
a global variable so that it can be accessed by the callback function and I also need to make sure that navItems
is populated before the callback function is executed.
I'm aware that the expect()
method takes care of promises in a nice clean way, I would like to implement something like this.