Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

311
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda