Selenium/WebdriverJs/Protractor promise chaining with page objects
You shouldn't try to make a PageObject a Promise. A PageObject is supposed to be a method/property factory and thus shouldn't be a constraint in the execution flow.I would keep it simple by returning an element with a property rather than trying to locate all the elements in a constructor:
describe('Suite', function() { it('should module title be ...', function() { let pageAdmin = AdminBaseBage.get(); let mContent = pageAdmin.mainContent; let titlePromise = mContent.getModuleTitle(); expect(titlePromise).toEqual('module title'); });});class MainContent { constructor() { } get element_module_title() { return element(By.css('#maincontent #moduleTitle')); } /** * Gets the title of the main content * * @returns {webdriver.promise.Promise<string>} */ getModuleTitle() { return this.element_module_title.getText(); }}
thanks for your input.
You are right that page objects should not constraint the execution flow. I will forget about trying to make a promise here :-).I also extracted the element initialization in the constrcturos to getter methods.
My main page object now consists of several page elements (LeftNavigation, TopMenu etc.) which I have created.Each of these page elements now access the WebElements they need (only via the getter methods).
class AdminBasePage extends BasePage { constructor{ super(); /** @type {LeftNavigation} */ this._leftNavigation = new LeftNavigation(); /** @type {TopMenu} */ this._topMenu = new TopMenu(); /** @type {PathNavi} */ this._pathNavi = new PathNavi(); /** @type {ContentTopBar} */ this._contentTopBar = new ContentTopBar(); /** @type MainContent*/ this._mainContent = new MainContent() } /** * @returns {Promise<AdminBasePage>} */ static getPage() { return browser.driver.get("index.php").then(function() { return new AdminBasePage(); }); } ....getters + other methods follow}
My test now looks as followed:
describe('module_checklist', function () { it('Check number of elements in list', function () { let page = AdminBasePage.getPage();//returns Promise<AdminBage> // check number of list rows page.then(function (templateListPage) { return templateListPage.mainContent.getArrListRows();//returns Promise<ListRow[]> }) .then(function (arrRows) { expect(arrRows.length).toEqual(2); }); //check total number in pagination page.then(function (templateListPage) { expect(templateListPage.mainContent.getPagination().getIntPaginationTotalNumber()).toEqual(2); }); });}