My website has a navigation menu with different items. I'm using a page model for end-to-end tests.
In one of my test specs, I need to call a function from the page model that does the following:
- Check that only one item in the menu has a specific class
- Return the index of that item
This is a snippet of my code:
describe('Main screen and global navigation', () => {
class MainPage {
load() {
browser.get('');
}
getMainMenuActiveItemIndex(): number {
let list = element.all(by.css('ul.menu li'));
// Need help iterating through
return 0;
}
navigate(itemClass: string) {
let menuItem = element(by.css('ul.menu li.' + itemClass));
menuItem.click();
}
}
let p = new MainPage();
beforeEach(function () {
p.load();
});
it('should navigate to subpages', () => {
expect(p.getMainMenuActiveItemIndex()).toBe(0);
p.navigate('invoices');
expect(p.getMainMenuActiveItemIndex()).toBe(1);
});
})
I understand that element...
returns a promise. However, I'm unsure how to resolve the promise before returning a value from the function.
Should I return a promise from the function instead of just a number?