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

341
Visualizações
How to properly write the values of an array into a JSON file using writeFile in Cypress?

I wanted to write the values of an array into a JSON file by using writeFile .

console.log(category);

cy.writeFile('cypress/fixtures/actionsName.json', category);

I've outputted the values of the category array. See below

Value of the array

My expected contents of the file would be something like this

enter image description here

However, upon writing the contents of the JSON file using writeFile, it only prints the top level array, not including its contents. Contents of the file

I'm not sure what I'm missing, I would really appreciate if someone can take a look. Thanks in advance!

UPDATE: As per request, below is the code used to populate the category array. Let me know if anything needs to be updated/optimized.

getActionName(){
    let category = new Array();
    let actions = new Array();

    //Get all action name and store to json
    cy.get('.linkbox').each(($category) => {

        //Get category name
        const text = $category.find('.heading').text();
        cy.log("Category: " + text);
        category.push(text);

        //Get each action name and push to the related category
        cy.wrap($category).find('ul li h2').each(($action) => {
            const text = $action.text();
            cy.log("Action: " + text);

            actions.push(text);
        }).then(() => {
            category[text] = actions;
            actions = [];
        })

    }).then(() =>{
        console.log(category);

        //!Only writes the top level array
        cy.writeFile('cypress/fixtures/actionsName.json', category);
    }) 
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's a bit hard to tell for sure from the screenshot, but I think the array has non-standard properties attached (an Array is an object).

Try converting,

const keys = ['Alert', 'Dynamic Elements', 'Frames and Windows', etc]
const output = keys.reduce((acc, item) => {
  acc[item] = category[item]
  return acc
}, {})
cy.writeFile('cypress/fixtures/actionsName.json', output)

Just looked at your posted code - the problem is as expected, you're attaching the lists to the array as properties. The above should work, or you can clean up the way you gather the lists.

getActionName(){
    //let category = new Array();  // change this to an object
    let category = {};
    let actions = new Array();

    //Get all action name and store to json
    cy.get('.linkbox').each(($category) => {

        //Get category name
        const text = $category.find('.heading').text();
        cy.log("Category: " + text);
        //category.push(text);    // don't need this

        //Get each action name and push to the related category
        cy.wrap($category).find('ul li h2').each(($action) => {
            const text = $action.text();
            cy.log("Action: " + text);

            actions.push(text);
        }).then(() => {
            category[text] = actions;
            actions = [];
        })

    }).then(() =>{
        console.log(category);

        //!Only writes the top level array
        cy.writeFile('cypress/fixtures/actionsName.json', category);
    }) 
}
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