I am currently working on writing e2e tests for an Ionic app using Protractor and Cucumber. I have implemented a page object pattern, but I am facing an issue where the call to count()
is returning 0
, even though I have waited for the elements to be present. Strangely, adding a sleep function makes it work. Here's a snippet of my step definition:
Then('my cases should be listed', function (callback) {
casesPage.isLoaded()
.then(() => {
expect(casesPage.numberOfFamilies())
.to.eventually.equal(20)
.and.notify(callback);
})
});
This is my page object code:
import { browser, $, $$, by, ElementFinder, ElementArrayFinder } from 'protractor';
export class CasesPage {
listOfFamilies: ElementArrayFinder;
private initializePromise: Promise<void>;
async initialize(): Promise<void> {
if(!this.initializePromise) {
return this.initializePromise = new Promise<void>(async (resolve) => {
this.listOfFamilies = $$('ul.families li.family');
return resolve();
});
}
}
get() {
return browser.get('/cases')
}
async isLoaded(): Promise<boolean> {
await this.initialize();
return this.listOfFamilies.isPresent();
}
async numberOfFamilies(): Promise<number> {
await this.initialize();
// browser.sleep(3000); Uncommenting this works
return this.listOfFamilies.count();
}
}
Any suggestions on how to fix this issue?