Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

312
Vistas
In Playwright POM how do you use page$$ in the constructor to avoid multiple hard coded selectors?

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.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

create locator variables first

 exports.MainPage = class MainPage extends BasePage {
   readonly menuitem: Locator;
 }
constructor(page){
  this.menuitem = page.$$('.menu-item .menu-item__label');
  .....
}

Use locator instead of $$. https://playwright.dev/docs/next/locators

https://playwright.dev/docs/next/api/class-page#page-query-selector

about 4 years ago · Juan Pablo Isaza Denunciar

0

Locators are basically Public Class Fields so I would suggest to declare outside constructor. The core idea is to declare the value once so that if needs to be changed later , can be done only in a single place to keep it DRY.

How about this as below?

     exports.MainPage = class MainPage extends BasePage {
      menuitem ='.menu-item .menu-item__label'
    
      constructor(page) {
        super(page);   
       
      }
    async countMenuItems(){
        let items = await this.page.$$(menuitem);          
        return items.length
      } 
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda