I am trying to follow the page object model using Playwright but I cant find a way to use the page.$$ in the constructor to get the 'menuitem' element/array and then reuse that locator in any functions I create. It looks like the only way to do it is to hard code the menuitem selector path into any functions that may need it (see 'countMenuItems' function below which works). Does anyone have any hints to help me move forwards without having to hard code the a selector each time?
exports.MainPage = class MainPage extends BasePage {
constructor(page) {
super(page);
this.relativeurl = "";
this.header = page.locator(".topbar");
//this does not work
//this.menuitem = page.$$('.menu-item .menu-item__label');
}
async navigate() {
await super.navigate(this.relativeurl);
}
async countMenuItems(){
//click menu
//this does work
let items = await this.page.$$('.menu-item .menu-item__label');
return items.length
}
}
I have just recently moved from using Protractor as my automation tool of choice where this kind of thing was fairly straight forward where you used element.all but struggling a bit with playwright. Any help much appreciated as I would prefer to only have to declare element once in pom.
create locator variables first
Use locator instead of $$. https://playwright.dev/docs/next/locators
https://playwright.dev/docs/next/api/class-page#page-query-selector