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

215
Visualizações
How to set multiple values to a single variable in .json file using cypress?

Here is my code to write fetched values in the .json file.

cy.readFile(filename).then((product) => {
                    for (var i = 1; i < lens; i++) {
                        cy.xpath('/html/body/app-root/div/mat-sidenav-container/mat-sidenav-content/app-configurable-product/div[1]/mat-table/mat-row[' + i + ']/mat-cell[2]').then((txt) => {
                            var namee = txt.text()
                            cy.log(namee)
                            product.product_name = namee
                            cy.writeFile(filename, product, {flag: 'a+'})
                        })
                    }
            })

I want to get data in the below .json format.

{
"product_name": [" Jam & Honey Girl's Regular Fit T-Shirt ", " Kids hoodie ", " Women tank top "]
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Perhaps use each instead of for loop

cy.readFile(filename).then((product) => {

  if (!product.product_name) product.product_name = [] // initial

  cy.xpath('/html/body/app-root/div/mat-sidenav-container/mat-sidenav-content/app-configurable-product/div[1]/mat-table/mat-row')
    .each($row => {
      cy.wrap($row).find('mat-cell').eq(2).invoke('text')
        .then(text => {
          product.product_name.push(text)
        })
    })
    .then(() => {
      cy.writeFile(filename, product)
    })
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You can select multiple rows and and convert them to array of texts

cy.xpath('/html/body/app-root/div/mat-sidenav-container/mat-sidenav-content/app-configurable-product/div[1]/mat-table/mat-row')
  .find('mat-cell').eq(2)
  .then($cells => {
    const texts = [...$cells].map(cell => cell.innerText)  // array of texts
    cy.readFile(filename).then((product) => {
      product.product_name = texts
      cy.writeFile(filename, product)
    })
  })
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